Back to Parent

// define a pin that I'll place pot on
 int potPin = A5;
 
 // create a variable to hold the pot reading
 int potReading = 0;
 
 // define a pin I'll place an LED on
 int ledPin = D2;
 
 // create a variable to store the LED brightness
 int ledBrightness = 0;
 
 //define a pin I'll place a button on
int buttonPin = D3;
 
 void setup() {
     
    // for input, I define the pushbutton as an input-pullup
    // this uses an internal pullup resistor to manage consistent reads from the device
    
    pinMode( buttonPin, INPUT_PULLUP ); // sets pin as input
    
     // set up the LED for output
     pinMode( ledPin, OUTPUT );
     
     // create a cloud variable of type integer
     // called 'light' mapped to photoCellReading
     Spark.variable( "pot", potReading );

}

void loop() {
    
    // find out if the button is pushed or not by reading from it
    int buttonState = digitalRead( buttonPin );
    
    // when the button is pushed, we will get a LOW signal
    // when the button is not pushed, we'll get a HIGH
    
    // set the LED on or off
    if( buttonState == LOW )
    {
        // turn the LED on
        digitalWrite ( ledPin, HIGH );
        
        // use analogRead to read the potentiometer reading
        // This gives us a value from 0 to 4095
        potReading = analogRead( potPin );
        
        // map this value into the PWM range (0-255) and store as the led brightness
        ledBrightness = map( potReading, 0, 4095, 0, 255 );
        
        // fade the LED to the desired brightness
        analogWrite( ledPin, ledBrightness );
        
        // wait 1/10th of a second and then loop
        delay(100);
        
        }else{
        // otherwise, turn the LED off
        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