xxxxxxxxxx
180
// Energy Sources Visualizer
// Created by Brandon Roots
// Conversion Data
// Source to 1 kilowatt-hour
// https://blueskymodel.org/kilowatt-hour
// Amount of fuel used per kWh
// https://www.eia.gov/tools/faqs/faq.php?id=667&t=8
let emitter, attractor, deflector, appliances, fontReg, b1, b2, c1, c2, c3, c4, c5, colorArray, lerp1, lerp2, watts;
/*
const sources = ['Coal', 'Natural Gas', 'Oil', 'Nuclear', 'Solar', 'Wind', 'Biomass'];
const sourcesToKWH = ['1.10', '7.48', '0.08', '', '', '', ''];
const sourcesUnits = ['Lbs', 'Sq Ft', 'Gallons', '', '', '', ''];
const sourceKWHToC02 =['909', '465', '821', '6', '105', '13', '1500'];
*/
let gramsC02 = 0;
let gramsCoal = 0;
let time = 0;
function preload() {
fontReg = loadFont('assets/ostrich-regular.otf');
img1 = loadImage('png/Toaster-Oven.png');
img2 = loadImage('png/Fridge.png');
img3 = loadImage('png/Portable-Washing-Machine.png');
img4 = loadImage('png/2015-MacBook-Pro.png');
img5 = loadImage('png/Hue-Lamp.png');
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
emitter = new Emitter();
attractor = new Attractor(0, 0, 100);
deflector = new Deflector(0, 0, 10);
appliances = new Appliances();
textFont(fontReg);
textAlign(CENTER, CENTER);
// Update time
time = millis();
// Set Watts
watts = 0;
wattsTime = millis();
wattsDelay = 5000;
wattsLive = true;
wattsLiveValue = 0;
wattsName = 'Laptop';
updateWatts();
// Add appliances
appliances.addAppliance('Toaster Oven', '1092', img1);
appliances.addAppliance('Washer', '200', img3);
appliances.addAppliance('Fridge', '100', img2);
appliances.addAppliance('Laptop', '42', img4);
appliances.addAppliance('Hue Lamp', '5', img5);
}
function draw() {
background(200);
//update grams C02
let diff = millis() - time;
time = millis();
gramsC02 += (diff/3600000)*(watts/1000)*0.909;
//update grams coal
gramsCoal += (diff/3600000)*(watts/1000)*353.34846;
// C02 Text
textSize(windowHeight / 25);
fill(200, 105, 0);
text(round(gramsC02, 4)+' g/C02', 0, -windowHeight/2.25);
// Watts Text
textSize(windowHeight / 10);
fill(0);
text(watts+'W', 0, 0);
// Watts Name Text
textSize(windowHeight / 25);
text(wattsName, 0, -40);
// Watts Live Text
if(millis() - wattsTime > wattsDelay && wattsLive){
updateWatts();
wattsTime = millis();
}
if(wattsLive){
textSize(windowHeight / 25);
text('LIVE', 0, 40);
}
// Coal Text
textSize(windowHeight / 25);
fill(0);
text(round(gramsCoal, 2)+' g/Coal', 0, windowHeight/2.25);
// Particles
emitter.run(attractor);
// Appliances
appliances.run();
// Draw coal
coal();
// Add particle
if(watts/2 > emitter.particles.length && millis()%2 > 1){
emitter.addParticle(random(-25, 25), random(windowHeight/3, windowHeight/4.5));
}
}
/*
function mousePressed(){
emitter.addParticle(random(-25, 25), random(windowHeight/3, windowHeight/4.5));
}
*/
function coal() {
// Coal bricks at bottom of screen
coalColor = map(watts%62.42, 0, 62.42, 50, 250)
pointLight(250, 250, 250, 0, 0, 0);
noStroke();
fill(coalColor);
push();
translate(0, (windowHeight/3)-20, 40);
rotateY(millis()/10000);
// Loop through coals
for(let i = 0; i < watts; i += 62.42*4){
rotateX(-0.9);
rotateZ(0.7);
if(i+62.42 > watts){
box(watts%62.42);
}else{
box(62.42);
}
translate(0, -10, -10);
}
pop();
// CO2 at top of screen
let C02box = watts;
ambientLight(250, 200, 0);
let orangeColor = "rgb(250, 200, 0)";
noStroke();
fill(orangeColor);
translate(0, (-windowHeight/3)+20, 40);
push();
rotateY(-millis()/10000);
rotateX(-0.6);
rotateZ(-0.2);
box(C02box/20);
translate(40, 5);
fill(orangeColor);
rotateX(-0.9);
box(C02box/30);
pop();
}
function updateWatts() {
// Fetch from this URL
const url = 'https://io.adafruit.com/api/v2/broots/feeds/watts?x-aio-key=dc5119bf917f4b9b96a464094c8e57a3';
fetch(url)
.then(res => res.json())
.then((out) => {
//console.log('Output: ', out.last_value);
watts = out.last_value;
wattsLiveValue = out.last_value;
}).catch(err => console.error(err));
}