Concept & Method
In this assignment, I am using two LEDs, one switch (digital), and one slider potentiometer (analog). The switch controls the entire circuit, and the potentiometer controls the brightness of the LEDs.
Implementation
Video Player
00:00
00:00
Video Player
00:00
00:00
Code:
#define LED_PIN 3
#define BUTTON_PIN 7
#define POTENTIO_PIN A1
byte lastButtonState = HIGH;
bool LEDOn = true;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState)
{
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
LEDOn = ! LEDOn;
}
}
if (LEDOn) {
int potentiometerValue = analogRead(POTENTIO_PIN);
int brightness = map(potentiometerValue, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
delay(100); // delay is in milliseconds
analogWrite(LED_PIN, LOW); // turn LED off (0V)
delay(100);
}
else {
digitalWrite(LED_PIN, LOW);
}
}
#define LED_PIN 3
#define BUTTON_PIN 7
#define POTENTIO_PIN A1
byte lastButtonState = HIGH;
bool LEDOn = true;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState)
{
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
LEDOn = ! LEDOn;
}
}
if (LEDOn) {
int potentiometerValue = analogRead(POTENTIO_PIN);
int brightness = map(potentiometerValue, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
delay(100); // delay is in milliseconds
analogWrite(LED_PIN, LOW); // turn LED off (0V)
delay(100);
}
else {
digitalWrite(LED_PIN, LOW);
}
}
#define LED_PIN 3 #define BUTTON_PIN 7 #define POTENTIO_PIN A1 byte lastButtonState = HIGH; bool LEDOn = true; void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { byte buttonState = digitalRead(BUTTON_PIN); if (buttonState != lastButtonState) { lastButtonState = buttonState; if (buttonState == HIGH) { // button has been released LEDOn = ! LEDOn; } } if (LEDOn) { int potentiometerValue = analogRead(POTENTIO_PIN); int brightness = map(potentiometerValue, 0, 1023, 0, 255); analogWrite(LED_PIN, brightness); delay(100); // delay is in milliseconds analogWrite(LED_PIN, LOW); // turn LED off (0V) delay(100); } else { digitalWrite(LED_PIN, LOW); } }