Back to Parent

int stock;
int greenled;
int redled;
int stockgreenled = D2;
int stockredled = D1;
int newsled = D0;
int newsbrightness = 0;

void stockdata(const char *event, const char *data){
  if(data){
    int stock = atoi(data);
    Serial.println(stock);
    if(stock>0){
      greenled = map(stock, 0, 500, 0, 255);
      analogWrite(stockredled, 0);
      analogWrite(stockgreenled, greenled);
      newsbrightnesscontrol();
      Serial.println(greenled);
    }if(stock<0){
      redled = map(stock, -500, 0, 255, 0);
      analogWrite(stockgreenled, 0);
      analogWrite(stockredled, greenled);
      newsbrightnesscontrol();
      Serial.println(redled);
    }if(stock == 0){
      greenled = 0;
      redled = 0;
      analogWrite(stockredled, redled);
      analogWrite(stockgreenled, greenled);
      newsbrightnesscontrol();
    }else{}
  }else{
    newsbrightnesscontrol();
  }
}

void setup(){
  Serial.begin(9600);
  pinMode(newsled, OUTPUT);
  pinMode(stockredled, OUTPUT);
  pinMode(stockgreenled, OUTPUT);
  Particle.function ("WhatsNew", nytMostPopular);
  Particle.subscribe("stock", stockdata);
  analogWrite(newsled, 255);
  analogWrite(stockredled, 255);
  analogWrite(stockgreenled, 255);
  delay(1000);
  analogWrite(newsled, 0);
  analogWrite(stockredled, 0);
  analogWrite(stockgreenled, 0);
}

void loop(){
  newsbrightnesscontrol();
}

int nytMostPopular(String WhatsNew){
  newsbrightness = newsbrightness + 10;
  if(newsbrightness>255){
    newsbrightness = 255;
  }
}

void newsbrightnesscontrol(){
  analogWrite(newsled, newsbrightness);
  delay (2000);
  if (newsbrightness >= 5){
  newsbrightness = newsbrightness - 5;
  }else{
    newsbrightness = 0;
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0