Back to Parent

Code for Lights Out Circuit
// Code samples used from following tutorials:
// http://ideate.xsead.cmu.edu/gallery/projects/manage_projects/lights-out/documentation
// http://diotlabs.daraghbyrne.me/5-getting-input/switches/

// We will be using D1 to control our LED
int ledPin = D1;

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

// Define a pin that we'll place the photo cell on
int photoCellPin = A0;

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

// Create a variable to store the LED brightness.
int ledBrightness = 0;

void setup()
{
  pinMode( switchPin , INPUT_PULLUP); // sets pin as input
  pinMode( ledPin , OUTPUT ); // sets pin as output
  Particle.variable("light", &photoCellReading, INT);
}

void loop()
{
   int switchState = digitalRead( switchPin );
   Serial.print(switchState);
  if( switchState == LOW )
  {
    photoCellReading = analogRead(photoCellPin);
    // Publish the value of the photoresistor's reading to the cloud
    Particle.publish("Light Value", String(photoCellReading), 60, PUBLIC);
    ledBrightness = map(photoCellReading, 1900, 3600, 0, 255);
    analogWrite(ledPin, ledBrightness);
    delay(100);
  } else {
  digitalWrite( ledPin, LOW);  }

}
Click to Expand

Content Rating

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

0