Skip to content

Instantly share code, notes, and snippets.

@jkeefe
Created November 6, 2013 03:25
Show Gist options
  • Save jkeefe/7330398 to your computer and use it in GitHub Desktop.
Save jkeefe/7330398 to your computer and use it in GitHub Desktop.
//// ARDUINO SKETCH ////
// Based on the NeoPixel "strandtest" example: https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
//LED Strip
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Serial Port
Serial.begin(9600);
}
void loop() {
// if there's any serial data available, read it:
while (Serial.available() > 0) {
// get the numbers of LEDs to light up from the
// PROCESSING sketch
int deblasio_light_up = Serial.parseInt();
int lhota_light_up = Serial.parseInt();
// so we can watch things in the Serial window
Serial.print(deblasio_light_up);
Serial.print(lhota_light_up);
Serial.print(22222);
// clear the strip
colorNow(strip.Color(0, 0, 0), 0); // black, no wait
// color the deblasio row blue from 0 to 59.
for(uint16_t i=0; i<deblasio_light_up; i++) {
strip.setPixelColor(i, strip.Color(0,0,255));
strip.show(); // light the next LED
delay(30); // give it a little drama
} // close for
// color the lhota row blue from 59 to 0.
for(uint16_t i=0; i<lhota_light_up; i++) {
strip.setPixelColor(60-i, strip.Color(255,0,0));
strip.show(); // light the next LED
delay(30); // give it a little drama
} // close for
// color the center LED yellow
strip.setPixelColor(30, strip.Color(120,120,0));
strip.show(); // light the next LED
} // close while
} // close loop
// Make a color instantly
void colorNow(uint32_t colr, uint16_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, colr);
}
strip.show();
delay(wait);
}
//// PROCESSING SKETCH ////
// based on example from http://processing.org/reference/loadJSONObject_.html
// import serial library
import processing.serial.*;
Serial port;
JSONObject json;
JSONObject bodies;
JSONObject mayors;
JSONObject contests;
JSONObject nyc;
JSONObject candidates;
JSONObject deblasio;
JSONObject lhota;
int lastMillis = 0;
void setup() {
// --- ran the following as a separate sketch
// --- be sure to notice that the list is space-separated!
// --- don't rely on line feeds
// // List all the available serial ports in the output pane.
// // You will need to choose the port that the Arduino board is
// // connected to from this list. The first port in the list is
// // port #0 and the third port in the list is port #2.
// println(Serial.list());
// Open the port that the Arduino board is connected to (in my case #11)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[11], 9600);
}
void draw() {
// check to see if 60 seconds have passed
if(millis()>lastMillis+60000)//been at least 60000 mills aka 60 secs
{
lastMillis=millis(); //update the last time we did anything
// json = loadJSONObject("NY-test.json");
json = loadJSONObject("http://project.wnyc.org/election2013/data/NY.json");
bodies = json.getJSONObject("bodies");
mayors = bodies.getJSONObject("Mayor");
contests = mayors.getJSONObject("contests");
nyc = contests.getJSONObject("New York City");
candidates = nyc.getJSONObject("candidates");
deblasio = candidates.getJSONObject("74536");
lhota = candidates.getJSONObject("74535");
String deblasio_test = deblasio.getString("ballot_name");
Float deblasio_pct_float = deblasio.getFloat("vote_percent");
// note that we take this in as a string because "Serial.write" doesn't do floats
String lhota_test = lhota.getString("ballot_name");
Float lhota_pct_float = lhota.getFloat("vote_percent");
// set the number of deblasio LEDs to light up
// as a percentage of 60 LEDs on the whole strip ...
float d_temp = 60 * (deblasio_pct_float / 100);
float l_temp = 60 * (lhota_pct_float / 100);
// add .5 because we want to round and the cast function just truncates
d_temp = d_temp + 0.5;
l_temp = l_temp + 0.5;
// set the number of LEDs to light up
// cast d_temp as an integer (so 8.7 will become 8)
String deblasio_light_up = str((int) d_temp);
String lhota_light_up = str((int) l_temp);
// write data to the screen
println(deblasio_test + " " + deblasio_pct_float + "% LEDs: " + deblasio_light_up);
println(lhota_test + " " + lhota_pct_float + "% LEDs: " + lhota_light_up);
// write the comma-separated values to the serial port
port.write(deblasio_light_up);
port.write(",");
port.write(lhota_light_up);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment