Created
December 26, 2013 23:53
-
-
Save pingud98/8140359 to your computer and use it in GitHub Desktop.
Snowflake power meter
This code takes in pulses from an arduino over serial and draws a snowflake on the screen for each pulse. It also passes the values into a MySQL database, if you give it the right information. it needs the bezier sql library and the mysql library to work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Based on an example by Tom Igoe | |
PrintWriter output; | |
import processing.serial.*; | |
import de.bezier.data.sql.*; | |
//import SQLibrary.*; // mysql database library | |
int xrnd = 0; | |
int yrnd = 0; | |
PFont fontA; | |
int xbig = 1000; | |
int ybig = 768; | |
int flakesize = 200; | |
Serial myPort; // The serial port: | |
String buff = ""; | |
int NEWLINE = 10; | |
MySQL msql; | |
void setup() { | |
// List all the available serial ports: | |
println(Serial.list()); | |
// I know that the first port in the serial list on my mac | |
// is always my Keyspan adaptor, so I open Serial.list()[0]. | |
// Open whatever port is the one you're using. | |
String portName = Serial.list()[1]; | |
myPort = new Serial(this, portName, 9600); | |
output = createWriter("datalog.txt"); | |
size(xbig,ybig); | |
background(0); | |
fontA = loadFont("CourierNew36.vlw"); | |
textAlign(CENTER); | |
textFont(fontA, 20); | |
String user = "insert username"; | |
String pass = "insert password"; | |
String database = "hackdb"; | |
msql = new MySQL( this, "insert yourserver here", database, user, pass ); | |
} | |
void draw() | |
{ | |
while (myPort.available() > 0) | |
serialEvent(myPort.read()); | |
} | |
void snowflakedraw() | |
{ | |
//randomly refresh the screen | |
if (random(0,200) > 170){ | |
// saveFrame("snowflakespaperx##.png"); | |
background(0,10); | |
} | |
//stroke(0, random(50,255)); | |
//translate(width/2, height/2); | |
translate(random(0+flakesize/2,width-flakesize/2), random(0+flakesize/2,height-flakesize/2)); | |
rotate(radians(random(0,360))); | |
//float itervar = random(0, 2*width); | |
for (int sweep = 1; sweep <random(5,50); sweep++){ | |
float spikelength = random(0, flakesize*.7); | |
float offset = random(0, flakesize*.7); | |
//draw the spines | |
stroke(255,10); | |
for (int i = 0; i<6; i++){ | |
rotate(radians(-60)); | |
line(0,0,0,flakesize/2); | |
} | |
//draw the flake bits | |
stroke(250,random(0,255)); | |
for (int i = 0; i<6; i++){ | |
rotate(radians(-60)); | |
line(0,spikelength/(sweep/2),spikelength/(sweep/2),offset/12); | |
line(0,spikelength/(sweep/2),-spikelength/(sweep/2),offset/12); | |
// delay(50); | |
} | |
} | |
} | |
void serialEvent(int serial) | |
{ | |
if (serial != NEWLINE) { | |
// Store all the characters on the line. | |
buff += char(serial); | |
} else { | |
// The end of each line is marked by two characters, a carriage | |
// return and a newline. We're here because we've gotten a newline, | |
// but we still need to strip off the carriage return. | |
if (buff.length()>1) { | |
buff = buff.substring(0, buff.length()-1); | |
// Parse the String into an integer. We divide by 4 because | |
// analog inputs go from 0 to 1023 while colors in Processing | |
// only go from 0 to 255. | |
// int val = Integer.parseInt(buff)/4; | |
// Clear the value of "buff" | |
println(buff); | |
snowflakedraw(); | |
//xrnd = int(random(0,xbig)); | |
//yrnd = int(random(0,ybig)); | |
// fill(random(180,255),random(0,255),random(0,255), 100); | |
// ellipse(xrnd, yrnd, 80, 80); | |
// font = loadFont("FFScala-32.vlw"); | |
// textFont(font); | |
// fill(random(0,50)); | |
// text("1kWh",xrnd-40,yrnd-5, 80, 50); | |
output.println(buff); | |
if ( msql.connect() ) | |
{ | |
msql.execute(buff);//"INSERT INTO yoursqltable VALUES (55, 2, 3)"); | |
// msql.next(); | |
// (<MsgNum>,<MeterNum>,<MeterAcc>) | |
// println( "number of rows: " + msql.getInt(1) ); | |
} | |
else | |
{ | |
// connection failed ! | |
} | |
buff = ""; | |
// Shift over the existing values to make room for the new one. | |
// for (int i = 0; i < 63; i++) | |
// values[i] = values[i + 1]; | |
// Add the received value to the array. | |
// values[63] = val*8; | |
} | |
} | |
} | |
void keyPressed() { // Press a key to save the data | |
output.flush(); // Write the remaining data | |
output.close(); // Finish the file | |
exit(); // Stop the program | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment