Back to Parent

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

// Our button wired to D0
int buttonPin = D3;
int buttonState; // store if the button has been pushed

int potPin = A5;
int potReadinIg = 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( buttonPin , INPUT_PULLUP); // sets pin as input (button

  // We also want to use the LED

  pinMode( ledPin , OUTPUT ); // sets pin as output

}

void loop()
{
 
   // find out if the button is pushed
   // or not by reading from it.
   buttonState = digitalRead( buttonPin );
   
   int potRead = analogRead( potPin); // 0-4095

  // 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( buttonState == LOW )
  {
    // turn the LED On
    //digitalWrite( ledPin, HIGH);
    int ledBrightness = map( potRead, 0, 4095, 0, 255);
    analogWrite( ledPin, ledBrightness); // 0-255
  
  }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