I was trying to develop a small system of lights which alternate between being on and off for a school project, but I am struggling with adding an on and off button to it. The furthest I have managed to get with this project is getting one light to be on by default then one button press turns on the blinking system, but I cant get it to turn off. Any suggestions as to how to go about this would be appreciated
What does Your plan for the flow of the logics, the happenings look like?
Planning is needed before hitting the keyboard.
Hi, @galacticgaming36
Welcome to the forum.
Please post the code you have, use code tags as described in the instruction page above.
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
Thanks.. Tom..
Hello galacticgaming36
Consider this sketch derived from the BlinkWithOutDelay example from the IDE.
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
const int LeftLedPin = 9 ;// the number of the LED pin
const int RightLedPin = 10 ;// the number of the LED pin
const int LeftButtonPin = A0;
const int RightButtonPin = A1;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 500; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(LeftLedPin, OUTPUT);
pinMode(RightLedPin, OUTPUT);
pinMode(LeftButtonPin, INPUT_PULLUP);
pinMode(RightButtonPin, INPUT_PULLUP);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
digitalWrite(LeftLedPin, ledState && (digitalRead(LeftButtonPin)?LOW:HIGH));
digitalWrite(RightLedPin, ledState && (digitalRead(RightButtonPin)?LOW:HIGH));
}
}
Have a nice day and enjoy coding in C++.
Ight, didn't quite know how to exactly do a schematic, so Imma just drop an image of the simulator I was using to test it so I knew I would not somehow break the Arduino:
Then the code is kinda just a mishmash of several different tutorials I utilized, one for the blinker mechanism and one for the button.
#define LED_PIN_1 11
#define LED_PIN_2 10
#define LED_PIN_3 9
#define BUTTON_PIN 4
#define LED_NUMBER 3
byte LEDPinArray[LED_NUMBER] = { LED_PIN_1,
LED_PIN_2,
LED_PIN_3 };
unsigned long debounceDuration = 50; //Made to make the button click only once
unsigned long lastTimeButtonStateChanged = 0;
byte lastButtonState = HIGH;
int LEDIndex = 0;
void initAllLEDs()//To make setup code smaller
{
for (int i = 0; i < LED_NUMBER; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}
void powerOnAllLEDs(bool powerOn) //Function meant to test the LEDS to see if they are working
{
for (int i = 0; i < LED_NUMBER; i++) {
if (powerOn) {
digitalWrite(LEDPinArray[i], HIGH);
}
else {
digitalWrite(LEDPinArray[i], LOW);
}
}
}
void powerOnSelectedLEDOnly(int index)
{
for (int i = 0; i < LED_NUMBER; i++) {
if (i == index) {
digitalWrite(LEDPinArray[i], HIGH);
}
else {
digitalWrite(LEDPinArray[i], LOW);
}
}
}
void toggleNextLED() //how the LEDs are being switched off and on
{
LEDIndex++;
if (LEDIndex >= LED_NUMBER) {
LEDIndex = 0;
}
powerOnSelectedLEDOnly(LEDIndex);
}
void setup()
{
initAllLEDs();
digitalWrite(LEDPinArray[LEDIndex], HIGH);
}
void loop()
{
unsigned long timeNow = millis();
if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = timeNow;
lastButtonState = buttonState; //checks button state
if (buttonState == HIGH) {
for(int i = 0; buttonState == HIGH; i++) //loops the function
{
delay(250);
toggleNextLED();
}
}
}
}
}
Hi,
You have a button that connects the digital input LOW when pressed, you have no pullup resitor to pull the digital input HIGH when open.
In fact you have no pinMode for the button;
Add;
pinMode(BUTTON_PIN, INPUT_PULLUP:
to your void setup()
It will turn on the internal pullup resistor, so you will not need to add one to your circuit.
Tom..
Alright, I added the pinMode, but it is still doing the same thing: one led is on before pressing the button, pressing button causes LEDs to cycle, the pressing button once more does not do anything. I need the button to stop the cycling. Am I missing a function or something? Any advice greatly appreciated.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.