Back to Parent

//pins 

int ledPin = D3; 

int switchPin = D4; 

int potPin = A5; 

//variable to store pot readings and brightness of the LED. 
int potReading = 0; 

int ledBrightness = 0; 

void setup()
{
    pinMode(switchPin , INPUT_PULLUP); 
    
    pinMode(ledPin, OUTPUT); 
    
    Particle.variable("pot" , potReading); 
    
}

void loop() 
{
    int switchState = digitalRead(switchPin); 
    
    if(switchState == LOW)
    {
        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
    { 
       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