Greetings, I'm doing a LED project with a Toggle button where I have two modes, one with all the lights on and the second with random blinking lights. The problem is that when I run the program it doesn't come out as expected. the leds keep blinking.
// 2015/03/29
/*
Demonstrates the use of an array to hold pin numbers.
Each time the push button is pressed, next LED in the
sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = A1;
/*const int buttonpin2 = A2;*/ // the number of the pushbutton pin
const int ledPin1 = 8; // the number of the LED pin
const int ledPin2 = 9; // the number of the LED pin
const int ledPin3 = 10; // the number of the LED pin
const int ledPin4 = 11; // the number of the LED pin
const int ledPin5 = 12; // the number of the LED pin
// variables will change:
int buttonState = 0;
int b2State = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;
int time = 2;
int Z = 0;
unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}
void AA(){
digitalWrite(random(8,12), HIGH);
delay(10); // Wait for 1000 millisecond(s)
digitalWrite(random(8,12), LOW);
}
void BB(){ digitalWrite(random(8,13), LOW); delay(500); // Wait for 1000 millisecond(s)
for (int i=8; i<13; i++) {
digitalWrite(i, HIGH);
} delay(300); // Wait for 1000 millisecond(s)
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (buttonPressCount % numberOfLED == 0)
for(int i = 8; i <= 12; i++){
digitalWrite(i, HIGH); // turn all LEDs on:
} else
if(buttonPressCount % numberOfLED == 1)
Z = 1;
delay(10);
buttonPressCount++;
delay(300);
}
if (Z = 1) {
// BlinkWithoutDelay
unsigned long newTime = millis();
if (newTime - oldTime >= 100) {
state = !state;
oldTime = newTime;
AA();
}
} else {
// no Blinking : just leave redLed as is
}
}
I improved the programming, but I still have a problem when all the leds are on, they turn off by themselves and do not want to stay on as planned for the programming.
// 2015/03/29
/*
Demonstrates the use of an array to hold pin numbers.
Each time the push button is pressed, next LED in the
sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = A1;
/*const int buttonpin2 = A2;*/ // the number of the pushbutton pin
const int ledPin1 = 8; // the number of the LED pin
const int ledPin2 = 9; // the number of the LED pin
const int ledPin3 = 10; // the number of the LED pin
const int ledPin4 = 11; // the number of the LED pin
const int ledPin5 = 12; // the number of the LED pin
// variables will change:
int buttonState = 0;
int b2State = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;
int time = 2;
int Z = 0;
unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}
void AA(){
digitalWrite(random(8,12), HIGH);
delay(10); // Wait for 1000 millisecond(s)
digitalWrite(random(8,12), LOW);
}
void BB(){ digitalWrite(random(8,13), LOW); delay(500); // Wait for 1000 millisecond(s)
for (int i=8; i<13; i++) {
digitalWrite(i, HIGH);
} delay(300); // Wait for 1000 millisecond(s)
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (buttonPressCount % numberOfLED == 0)
for(int i = 8; i <= 12; i++){
digitalWrite(i, LOW); // turn all LEDs on:
Z = 0;
} else
if(buttonPressCount % numberOfLED == 1)
for(int i =8; i <= 12; i++){
digitalWrite(i, HIGH);
Z = 0;
}
else
if(buttonPressCount % numberOfLED == 2)
Z = 1;
delay(10);
buttonPressCount++;
delay(300);
}
if (Z == 1) {
// BlinkWithoutDelay
unsigned long newTime = millis();
if (newTime - oldTime >= 100) {
state = !state;
oldTime = newTime;
AA();
}
} else {
for(int i =8; i <= 12; i++){
digitalWrite(i, LOW);
// no Blinking : just leave redLed as is
}
}
}
I have been running your code because it is easier to see what it does than to try and read it.
A few points
random(8,12)
will never be 12. Only 8, 9, 10 and 11. So you top LED won't be involved the way you think.
Look up random. google
arduino random function
This
digitalWrite(random(8,12), HIGH);
delay(10); // Wait for 1000 millisecond(s)
digitalWrite(random(8,12), LOW);
turns one random LED on, and another, probably not the same one, off. Is that what you want?
Z starts out as 0. Once it is set to 1, nothing ever makes it 0 again. Ever. Is that what you want?
BB() is never used. Where do you mean to use it?
Do you know how to use the serial monitor? It is invaluable - you can use serial printing all over your code to print the values of variables and confirm that your program is where you think it is, and that the flow through the program that key variables informs is correct, that is to say, is what you expected and wanted.
I see at the top of the code
// 2015/03/29
I assume you haven't been working on this for almost eight years. Please the original code that you started with, before you began attempting to bend it to your will.
the function BB(); At first I thought to use it but I discarded it later.
I want the programming to have two modes
mode 1 where all the leds are on
mode 2 where a random LED lights up in a loop
mode 3 all leds off
And yes, I don't have much experience in this. I've only been learning to program this system for a year and I want to learn more
Just in case the code is a modification of one that I found on the internet that was for a single blinking led and I wanted to modify it for two. but add the counts that you take from another code.
buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.
looks like you have 3 modes
possible problems
the Z == 0 condition near the end presumably always turns all the leds on regardless of what happens above (i.e ==1)
in AA, you set a random LED to HIGH and wait only 10 msec (despite the comment saying 1000 msec) and then set a random LED to LOW without waiting. 1) you probably want a delay after each digitalWrite() and 2) not sure if you want to toggle the same LED in both case, in which case, the random value needs to be the same in both writes
i made a modified version a its working.
have a little problem because the button is press in 2 to 3 second for responding
// 2015/03/29
/*
Demonstrates the use of an array to hold pin numbers.
Each time the push button is pressed, next LED in the
sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = A1;
/*const int buttonpin2 = A2;*/ // the number of the pushbutton pin
const int ledPin1 = 8; // the number of the LED pin
const int ledPin2 = 9; // the number of the LED pin
const int ledPin3 = 10; // the number of the LED pin
const int ledPin4 = 11; // the number of the LED pin
const int ledPin5 = 12; // the number of the LED pin
// variables will change:
int buttonState = 0;
int b2State = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;
int time = 2;
int Z = 0;
/*for second button A2*/
const int buttonPin2 = A2;
unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT);
Serial.begin(9600);
}
void AA(){
digitalWrite(random(8,12), HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(random(8,12), LOW);
delay(1000);
}
void BB(){ digitalWrite(random(8,13), LOW); delay(500); // Wait for 1000 millisecond(s)
for (int i=8; i<13; i++) {
digitalWrite(i, HIGH);
} delay(300); // Wait for 1000 millisecond(s)
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (buttonPressCount % numberOfLED == 0)
for(int i = 8; i <= 12; i++){
digitalWrite(i, LOW); // turn all LEDs on:
Z = 2;
} else
if(buttonPressCount % numberOfLED == 1)
for(int i =8; i <= 12; i++){
digitalWrite(i, HIGH);
Z = 0;
}
else
if(buttonPressCount % numberOfLED == 2)
Z = 1;
delay(10);
buttonPressCount++;
delay(300);
}
if (Z == 1) {
// BlinkWithoutDelay
unsigned long newTime = millis();
if (newTime - oldTime >= 100) {
state = !state;
oldTime = newTime;
AA();
}
} else {
if(Z == 2){
for(int i =8; i <= 12; i++){
digitalWrite(i, LOW);
// no Blinking : just leave redLed as is
}
}
}
Serial.println(Z);
}
i send the modified code and device according to yourself specification
// 2015/03/29
/*
Demonstrates the use of an array to hold pin numbers.
Each time the push button is pressed, next LED in the
sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = A1;
/*const int buttonpin2 = A2;*/ // the number of the pushbutton pin
const int ledPin1 = 8; // the number of the LED pin
const int ledPin2 = 9; // the number of the LED pin
const int ledPin3 = 10; // the number of the LED pin
const int ledPin4 = 11; // the number of the LED pin
const int ledPin5 = 12; // the number of the LED pin
// variables will change:
int buttonState = 0;
int b2State = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;
int time = 2;
int Z = 0;
/*for second button A2*/
const int buttonPin2 = A2;
unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT);
Serial.begin(9600);
}
void AA(){
digitalWrite(random(8,12), HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(random(8,12), LOW);
delay(1000);
}
void BB(){ digitalWrite(random(8,13), LOW); delay(500); // Wait for 1000 millisecond(s)
for (int i=8; i<13; i++) {
digitalWrite(i, HIGH);
} delay(300); // Wait for 1000 millisecond(s)
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (buttonPressCount % numberOfLED == 0)
for(int i = 8; i <= 12; i++){
digitalWrite(i, LOW); // turn all LEDs on:
Z = 2;
} else
if(buttonPressCount % numberOfLED == 1)
for(int i =8; i <= 12; i++){
digitalWrite(i, HIGH);
Z = 0;
}
else
if(buttonPressCount % numberOfLED == 2)
Z = 1;
delay(10);
buttonPressCount++;
delay(300);
}
if (Z == 1) {
// BlinkWithoutDelay
unsigned long newTime = millis();
if (newTime - oldTime >= 100) {
state = !state;
oldTime = newTime;
AA();
}
} else {
if(Z == 2){
for(int i =8; i <= 12; i++){
digitalWrite(i, LOW);
// no Blinking : just leave redLed as is
}
}
}
Serial.println(Z);
}