Back to Parent

// We will be using D2 to control our LED
int redPin = D2;
int bluePin = D3; 

// Our button wired to D0
int switchPin = D4;

// Photoresistor
int photoCellPin = A0; 

// Create a variable to hold the light reading
int photoCellReading = 0; 

//Create a variable to store the Blue LED brightness
int blueLedBrightness = 0; 

//Create a variable to store the Red LED brightness
int redLedBrightness = 0; 

void setup()
{

  // For input, we define the
  // pushbutton as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device

  pinMode( switchPin , INPUT_PULLUP); // sets pin as input

  // We also want to use the LED

  pinMode(redPin, OUTPUT);  // sets pin as output
  pinMode(bluePin, OUTPUT); // sets pin as output
  
  
  // Create a cloud variable of type integer
  // Called 'light' mapped to photoCellReading
  Particle.variable("light", &photoCellReading, INT); 

}

void loop()
{
    // find out if the button is pushed
    // or not by reading from it.
    int switchState = digitalRead( switchPin );
    
    
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    photoCellReading = analogRead(photoCellPin);
    
    
    // Map this value into the PWM range (0-255) 
    // and store as the led brightness
    redLedBrightness = map(photoCellReading, 3000, 4000, 0, 255); 
    blueLedBrightness = map(photoCellReading, 3000, 3999, 0, 255); 


  // remember that we have wired the pushbutton to
  // ground and are using a pulldown resistor
  // that means, when the button is pushed,
  // we will get a LOW signal
  // when the button is not pushed we'll get a HIGH

  // let's use that to set our LED on or off

  if (switchState == LOW & photoCellReading >=3800)
  {
        // turn the LED On
        digitalWrite(bluePin, HIGH); 
        digitalWrite(redPin, LOW); 
  } 
  else if (switchState == LOW & photoCellReading >= 0 & photoCellReading < 3800)
  {
        digitalWrite(redPin,  HIGH);
        digitalWrite(bluePin, LOW); 
  }
  else { 
    // otherwise
    // turn the LED Off
    digitalWrite(redPin, LOW);
    digitalWrite (bluePin, LOW); 
  }
}
Click to Expand

Content Rating

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

0