Back to Parent

// Include Particle Device OS APIs
#include "Particle.h"

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

//PINS
int flexPin = A0;
int redLED = D2;
int yellowLED = D3;
int greenLED = D4;
int buttonPin = D5;

//Va;irables

int flexRead = 0;
int ledOn = 0;
//bool buttonState = FALSE;

// setup() runs once, when the device is first turned on
void setup() {
    
    pinMode( buttonPin , INPUT_PULLUP);
  
    pinMode(redLED, OUTPUT);
    pinMode(yellowLED, OUTPUT);
    pinMode(greenLED, OUTPUT);
  
    Particle.variable("flex", flexRead);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  flexRead = analogRead(flexPin);
  ledOn = map(flexRead, 0, 10, 1, 3);
 /*
  if(flexRead < 2) ledOn = 1;
  else if (flexRead < 3) ledOn = 2;
  else ledOn = 3;
  */
  Particle.publish("flex", String(flexRead));
  int buttonState = digitalRead( buttonPin );
    if( buttonState == LOW )
    {
      if (ledOn == 3){
          digitalWrite(redLED, HIGH);
          digitalWrite(yellowLED, LOW);
          digitalWrite(greenLED, LOW);
      }//red ON
      else if (ledOn == 2){
          digitalWrite(redLED, LOW);
          digitalWrite(yellowLED, HIGH);
          digitalWrite(greenLED, LOW);
      }//yellow ON
      else if (ledOn == 1){
          digitalWrite(redLED, LOW);
          digitalWrite(yellowLED, LOW);
          digitalWrite(greenLED, HIGH);
      }//green ON
    }else{
        digitalWrite(redLED, LOW);
        digitalWrite(yellowLED, LOW);
        digitalWrite(greenLED, LOW); 
        
    }
  
    delay(100);
}
Click to Expand

Content Rating

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

0