As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Wednesday, June 10, 2015

Weather Lamp Final Code [Jessica and Gloria]

Arduino Not Reading the Serial Monitor Issue 



I encountered the problem where delays interfered with reading the serial output (the Arduino wouldn't register a button press while the program was delayed). I tried to write the neopixel light code without delays (similar to the blink without delay tutorial) but I couldn't figure it out so that we could still get the desired effect/lighting that we wanted. My solution was to just make the Arduino read the serial input every so often in the neopixel display functions.  It's definitely not the best/most efficient code, but at least it's working :)

Processing 

import processing.serial.*;
import com.onformative.yahooweather.*;

Serial myPort;  // Create object from Serial class

YahooWeather weather;
int updateIntervallMillis = 30000; 

void setup() 
{
   
  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  
  size(700, 300);
  fill(0);
  textFont(createFont("Arial", 14));
  // 2442047 = the WOEID of Berlin
  // use this site to find out about your WOEID : http://sigizmund.info/woeidinfo/
  weather = new YahooWeather(this, 12798949, "c", updateIntervallMillis);
}

void draw() {
  weather.update();
  background (255);
  text("City: "+weather.getCityName()+"; Region: "+weather.getRegionName()+"; Country: "+weather.getCountryName()+"; Last updated: "+weather.getLastUpdated(), 20, 20);
  text("Lon: "+weather.getLongitude()+" Lat: "+weather.getLatitude(), 20, 40);
  text( "Weather Condition:" + weather.getWeatherCondition(), 20, 60);
  text( "Condition Code:" + weather.getWeatherConditionCode(), 20, 80);  
  //text("WindTemp: "+weather.getWindTemperature()+" WindSpeed: "+weather.getWindSpeed()+" WindDirection: "+weather.getWindDirection(), 20, 60);
  //text("Humidity: "+weather.getHumidity()+" visibility: "+weather.getVisibleDistance()+" pressure: "+weather.getPressure()+" rising: "+weather.getRising(), 20, 80);
  //text("Sunrise: "+weather.getSunrise()+" sunset: "+weather.getSunset(), 20, 100);
  
   String condition = weather.getWeatherCondition();
    println(condition);
   
   //if (condition.equals("Fair"))
   
   if (condition.contains("Fai")){
   myPort.write('1');      
   println("1");   //send a 1
   }
   
   else if(condition.contains("Cloudy")){
     myPort.write('3');
     println("3");
   }
   
   else if(condition.contains ("Rain") ||condition.contains("Storms")){
     myPort.write('2');
     println("2"); 
   }
   
   else if (key == 'p') {
     myPort.write('5');
     println("off");
   } 
}
   
 public void keyPressed() {
  if (key == 's') {
    weather.setWOEID(12798949); //seattle
  }
  if (key == 'r') {
    weather.setWOEID(44418); //London
  }
  if (key == 'm') {
    weather.setWOEID (90897019); //Mawsynram
  }
  if (key == 'h'){
    weather.setWOEID (2423945); //Honolulu
  }
  if (key == 'd') {
    weather.setWOEID (23509346); //Death Valley
  }
  if (key == 'n'){
    weather.setWOEID (2459115); //snow Solden
  }
}
   


Arduino

#include <NS_Rainbow.h>
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h> //random gen for TWINKLE

#define PIN 9
#define N_CELL 16 
#define PIXEL 60 //for adafruit 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRB + NEO_KHZ800);
NS_Rainbow ns_stick = NS_Rainbow(N_CELL,PIN);

byte flag_a = 1, flag_b = 0; //FOR TWINKLE
unsigned int cnt = 0, seed = 0;  // seed: used to initialize random number generator
const unsigned int maxCycle = 1200;  // go to next state when cnt >= maxCycle

 char val; // Data received from the serial port
 //int ledPin = 13; // Set the pin to digital I/O 13
  int ledPin = 9; 
  boolean flag = true; 
  int time = millis();

void setup() {
   //pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
   Serial.begin(9600); // Start serial communication at 9600 bps
   ns_stick.begin();
   ns_stick.setBrightness(255);
 }
  


void loop() {
  
 // digitalWrite(ledPin, LOW);
  // if (Serial.available()) 
   //{ // If data is available to read,
   ns_stick.setBrightness(255);
    
     Serial.read();
     val = Serial.read(); // read it and store it in val
   
   if (val == '1') {              //SUNNY WEATHER 
     //digitalWrite(ledPin, HIGH); // turn the LED on
     //ns_stick.setBrightness(255);
     rainbowfade(); //Gets stuck
     Serial.read();
   } else if (val == '2') {        //rainy Weather 
     raintwinkle();
     Serial.read();
   } else if (val == '3'){ //cloudy
     adafade();
     Serial.read();
   } else if (val == '5'){
    black();
     Serial.read();
   }
     
   
   //delay(10); // Wait 10 milliseconds for next reading
   //Serial.flush();
}

void rainbowfade() {    //Sunny Weather
  for(int i=0; i<2; i++) {
    rainbow(30);    // interval: 30ms
    Serial.read();
  } 
}
  
  
void rainbow(uint16_t interval) {    //Sunnt Weather pt.2
  Serial.read();
  uint16_t n = ns_stick.numCells();
  for(uint16_t j=0; j<255; j++) {  // one cycle j2 used to be 255
    Serial.read();
    for(uint16_t i=0; i<n; i++) {
      Serial.read();
      byte r_pos = (((i<<8) -  i) / n + j) % 0xFF;
      byte sect = (r_pos / 0x55) % 0x03, pos = (r_pos % 0x55) * 0x03;
      Serial.read();
  
      switch(sect) {
      case 0: 
        Serial.read();
        ns_stick.setColor(i,ns_stick.RGBtoColor(0xFF - pos, pos, 0x00)); break;
      case 1: 
        Serial.read();
        ns_stick.setColor(i,ns_stick.RGBtoColor(0x00, 0xFF - pos, pos)); break;
      case 2:
        Serial.read();
        ns_stick.setColor(i,ns_stick.RGBtoColor(pos, 0x00, 0xFF - pos)); break; 
      }  
      Serial.read();
    }
    ns_stick.show();
    delay(interval);
    Serial.read();
    
    //if (val !== 1) {
    //break;  
    //}
   }
}

void blue() {        //Stays on color (blue)
   for (int i=0; i<N_CELL; i++){
    ns_stick.setColor(i, 6 , 101, 196);
    ns_stick.show();
    Serial.read();
   }
}
 void bluefade (uint16_t interval) {
   uint16_t n = ns_stick.numCells();
  for(uint16_t j=0; j<255; j++) {  // one cycle j2 used to be 255
    for(uint16_t i=0; i<n; i++) {
      byte r_pos = (((i<<8) -  i) / n + j) % 0xFF;
      byte sect = (r_pos / 0x55) % 0x03, pos = (r_pos % 0x55) * 0x03;
  
      switch(sect) {
      case 0: 
        ns_stick.setColor(i,ns_stick.RGBtoColor(0xFF - pos, pos, 0x00)); break;
      case 1: 
        ns_stick.setColor(i,ns_stick.RGBtoColor(0x00, 0xFF - pos, pos)); break;
      case 2:
        ns_stick.setColor(i,ns_stick.RGBtoColor(pos, 0x00, 0xFF - pos)); break;
      }  
    }
    ns_stick.show();
    delay(interval);
    Serial.read();  
}
 }
void adafade () {    //Cloudy Weather
  int R = 0;
  int G = 0;
  int B = 0;
  int finCount=5;
  int foutCount=5;
  int Rset = 125;
  int Gset = 125;
  int Bset = 125;
  int waitT = 5;
  //Fade in
  while(1){ //using an inf loop to be more custom.
  //Protect the strand from higher then 255 values
  if(R>255 || G>255 || B>255) { break; } //DO NOT DELETE OR ALTER THIS LINE.
    //break the inf loop if the color is higher then what its set at.
    if (R>Rset+1 && G>Gset+1 && B>Bset+1)  { 
      //ReSet the RGB to set values. 
      R=Rset;
      G=Gset;
      B=Bset;
      break; 
    } 
    //update the strip
    for(int i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(R, G, B));
      strip.show();
      delay(0);
    }
    //increase by the set amount
    R=R+finCount;
    G=G+finCount;
    B=B+finCount;
    delay(waitT);
  }
  //Fade Out
  while(1){ //using an inf loop to be more custom.
  //Protect the strand from higher then 255 values
  if(R>255 || G>255 || B>255) { break; } //DO NOT DELETE OR ALTER THIS LINE.
  //break the inf loop if the color is off
    if (R<100 && G<100 && B<10)  { 
      //ReSet the RGB to 0 values. 
      R=100;
      G=100;
      B=100;
      break; 
    } 
    //update the strip
    for(int j=0; j<strip.numPixels(); j++) {
      strip.setPixelColor(j, strip.Color(R, G, B));
      strip.show();
      delay(0);
      Serial.read();
    }
    //Decrease by the set amount
    R=R-foutCount;
    G=G-foutCount;
    B=B-foutCount;
    delay(waitT);
  }
}

void raintwinkle() {    //for Rainy Weather
 ns_stick.setBrightness(256);
 for (int i = 0; i < 8; i++) {
    seed += analogRead(i);
    Serial.read();
  }
  seed += EEPROM.read(0);  // get part of the seed from EEPROM
  randomSeed(seed);
  EEPROM.write(0, random(256));
  delay(100);
  ns_stick.begin();
  ns_stick.clear();
  ns_stick.show();
  ns_stick.setBrightness(128);
  unsigned char sect, color;
  
  if(flag_a) {  
    sect = cnt / (maxCycle / 4);
    switch(sect) {
      case 0: 
        twinkleTwinkle(0, 1, 0);  // only white
        break;
      case 1: 
        twinkleTwinkle(0, 2, 0);  // white and red
        break;
      case 2: 
        twinkleTwinkle(1, 2, 0);  // red, and green
        break;
      default: 
        // red, green, blue, cyan, magenta, yellow
        twinkleTwinkle(1, 6, cnt > maxCycle - 100);
    }
    ns_stick.show();
  }
}

void fade(unsigned char *val, unsigned char fadeTime) { //Twinkle pt2
  if (*val) {
    unsigned char subAmt = *val >> fadeTime;
    if (subAmt < 1)
      subAmt = 1;
    *val -= subAmt;
  }
}

void twinkleColorAdjust(unsigned char *color) { //Twinkle pt 3
  if (*color == 255) {
    *color = 254;
  }
  else if (*color % 2) {
    *color = *color * 2 + 1;
  }
  else if (*color > 0) {
    fade(color, 4);
    if (*color % 2) {
      (*color)--;
    }
  }
}


//Twinkle pt4
void twinkleTwinkle(unsigned char minVal, unsigned char numColors, unsigned char noNewBursts) {
  for (unsigned int i = 0; i < N_CELL; i++) {
    uint16_t base = i * 3;
    twinkleColorAdjust((ns_stick.getCellsAddr()+base));
    twinkleColorAdjust((ns_stick.getCellsAddr()+base+2));
    twinkleColorAdjust((ns_stick.getCellsAddr()+base+1));
  }
  
  if (noNewBursts) return;
  
  for (unsigned int i = 0; i < 4; i++) {
    int j = random(N_CELL);
    uint32_t color = ns_stick.getColor(j);
    
    if (color) break;
    
    switch (random(numColors) + minVal) {
      case 0:
        ns_stick.setColor(j, 80, 100, 182);  // white RAINY COLOR
        break;
      case 1:
        ns_stick.setColor(j, 255,   0,   0);  // red
        break;
      case 2:
        ns_stick.setColor(j,   0, 255,   0);  // green
        break;
      case 3:
        ns_stick.setColor(j,   0,   0, 255);  // blue
        break;
      case 4:
        ns_stick.setColor(j, 255, 255,   0);  // yellow
        break;
      case 5:
        ns_stick.setColor(j,   0, 255, 255);  // cyan
        break;
      case 6:
        ns_stick.setColor(j, 255,   0, 255);  // magenta
        break;
      default:
        ns_stick.setColor(j, 255, 255, 255);  // white
    }
  }
}
  
void stick() {
  
   unsigned int   t = 500;   // t: delay time
  Serial.read();
  ns_stick.setColor(0, 255,   0,   0);      // Red
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(0,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(1, 162,  93,   0);      //   
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(1,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(2,  66, 189,   0);      // 
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(2,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(3,   0, 255,  30);      // 
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(3,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(4,   0, 129, 126);      // 
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(4,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(5,   0,  33, 222);      // 
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(5,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(6,  63,   0, 192);      // 
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(6,   0,   0,   0);      // Black (clear)
  ns_stick.setColor(7, 159,   0,  96);      // 
  ns_stick.show();
  Serial.read();
  delay(t);
  ns_stick.setColor(7,   0,   0,   0);      // Black (clear)
  ns_stick.show();
  //delay(t);
  Serial.read();
}  
      
void black() {        //Stays on black
   for (int i=0; i<N_CELL; i++){
    ns_stick.setColor(i, 0 , 0, 0);
    ns_stick.show();
    Serial.read();
   }
}      
      
      //ns_stick.clear();
    //ns_stick.setBrightness(0);




No comments:

Post a Comment