running two LEDs with two buttons - arduino

I am new to Arduino programing. I am trying to get two buttons to light two LEDs like so. press button1 turn on light 1 turn off light 2, button2 turn on light 1 and 2. like indicators for speed selection.
this is the code I have so far.
const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int LED1 = 9;
const int LED2 = 10;
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop(1)
{
BUTTONstate1 = digitalRead(BUTTON1);
if (BUTTONstate1 == HIGH);
digitalWrite(LED1, HIGH);
digitalwrite(LED2, low);
}
void loop(2)
{
BUTTONstate2 = digitalRead(BUTTON2);
if (BUTTONstate2 == HIGH)
digitalWrite(LED1, HIGH);
digitalwrite(led2, HIGH);
}
when i run the code I get ERROR 19:10 error: variable or field 'loop' declared void. it is the line after void loop(1) and void loop(2).

void loop(1) and void loop(2) don't make sense.
You only implement void loop() and handle both buttons in that function.
Arduino will call loop() in an infinite loop over and over. Having two loop functions doesn't make sense and causes compiler errors.
If you want to have that functionality in two different functiosn implement them using different names and call both of them inside loop()

I've never seen someone uses two void loops at one time. You have to use only one void loop(). Here is an example:
void loop() {
BUTTONstate1 = digitalRead(BUTTON1);
BUTTONstate2 = digitalRead(BUTTON2);
if (BUTTONstate1 == HIGH) {
digitalWrite(LED1, HIGH);
digitalwrite(LED2, LOW);
}
if (BUTTONstate2 == HIGH) {
digitalWrite(LED1, HIGH);
digitalwrite(LED2, HIGH);
}
}
You also make another error here. Don't ever print ; after if()

Related

Countdown once event is triggered

I'm just getting started with my Arduino and I want to operate a relay for a certain amount of time once I press a contact switch, but still be able to stop the it again at any time by pressing the button again. I've got the press the button to start and stop working Ok, but can't get the timer bit to work at all. Below is what I've got working so far.
TIA.
void setup()
{
Serial.begin(9600);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
Serial.print("Pin 7 ");
Serial.println(digitalRead(7));
if (digitalRead(6) == LOW && digitalRead(7) == HIGH) {
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(250); // Wait for 250 millisecond(s)
}
if (digitalRead(6) == HIGH && digitalRead(7) == HIGH) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(250); // Wait for 250 millisecond(s)
}
}
bool buttonState = 0, buttonStateBefore = 0;
bool toggle = 0;
unsigned long timeNow = 0;
buttonState = digitalRead(buttonPin);
if(buttonState > buttonStateBefore) {
toggle = !toogle; // negates the toogle
timeNow = millis(); // milliseconds to the time you press the button
}
buttonStateBefore = buttonState;
if(toggle && millis() < timeNow + delayTime) {
// relay on
}
else {
// relay off
}
Explanation:
When you press the button, buttonState beign now one, is bigger than the buttonStateBefore. So the if-statement is true. The variable toggle is now true and the time is set to the amount of milliseconds after starting the arduino. After the first if the buttonStateBefore is set to be the buttonState, to not toggle the toggle variable every circle. The second if-statement checks if the toggle variable is true and if the time has not exceeded. If one of the two things is not the case, the relay is turned off.
I hope this is helpful.
P.s. You can shorten your code if you use a for-loop for the digitalWrite
for(int i = 3; i < 7; i++) {
digitalWrite(i, true);
}

Arduino: wait for serial input

I am writing a simple arduino code. I have two leds and corresponding two switches. When one switch is pressed one led is on and other is off. Then there is a Serial.read function, which reads reset from the computer. Then both switch are off. After that other switch is pressed and other led is on. My problem is when one switch is on other shouldn't be working till the Serial.read happens.But in my case, when led1 is on if I press switch2 led2 is on and led1 is off. But that is not my desired operation. I want to make the logic that when led1 is on if I press switch2 led2 shouldn't be on and wait for the Serial.read to happen. Here is my code. I need to know what should be the correction in the logic:
int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1=2;
int d2=3;
int reset1 = 0;
int reset2= 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
d1=digitalRead(2);
d2=digitalRead(4);
if (d1==1)
{
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
if (Serial.available() > 0)
{
Serial.write(1);
}
if (Serial.available() > 0)
{
reset1 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
}
else if (d2==1)
{
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
if (Serial.available() > 0)
{
Serial.write(2);
}
if (Serial.available() > 0)
{
reset2 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
}
}
You seem to be using some asynchronous code structure. This is a good thing, but I am not really sure if this is what you intended to do.
In its current state, the code will continue looping over and over, checking if one of the buttons is pressed.
Now, there is two ways of achieving this:
Either make a proper state machine design, which would be the preferred way
Or just wait for the serial to be available at some point.
For the second solution, you could replace
if (Serial.available() > 0)
{
reset1 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
by
while (Serial.available() <= 0)
{}
reset1 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
essentially doing nothing while there is nothing on the serial port. Of course, this will completely freeze the rest of the program, and isn't very flexible.
For the first way, there is once again multiple ways of doing it. Since you seem not to be familiar with C programming (no offense), one of the easiest ways of doing it would be to change your if (d1==1) and else if (d2==1) statements by if (d1==1 && serial_read == false) and else if (d2==1 && serial_read == true). Then, at the top of the program, add:
int serial_read = false;
if(Serial.available() > 0)
{
reset1 = (Serial.read());
serial_read = true;
}
This is the basic idea. I will let you sort out the various bugs and improvements (such as setting serial_read to false again) as an exercise.
I also strongly encourage you to read a bit about programming in general, and C programming in particular. I also advise you to try to stick to some convention for indenting your code. There is nothing worse than code with mixed indentation.
This is the answer.
int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1 = 0;
int d2 = 0;
int buzzer;
char reset1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
d1 = digitalRead(2);
d2 = digitalRead(4);
if (d1 == 1 && buzzer == 0) {
digitalWrite(motorled1, HIGH);
buzzer = 1;
} else if (d2 == 1 && buzzer == 0) {
digitalWrite(motorled2, HIGH);
buzzer = 2;
}
if (Serial.available() > 0) {
Serial.write(buzzer);
reset1 = Serial.read();
buzzer = 0;
if (reset1 == 'R') {
Serial.println("LED is off");
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
}
}

athletic response light trainer by arduino

currently I am trying to develop a light response trainer by arduino , as a beginning I used 3 led and 3 push button ,the led must work randomly and when the ledx is flash the user press push bottonx and so on
of course I must use approximate sensor or something similar to be more reliable
when uploading following code all leds continuous week glow (flash) what the problem? thanks for help.
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb1 = 4;
int pb2 = 5;
int pb3 = 6;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb1, INPUT);
pinMode(pb2, INPUT);
pinMode(pb3, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0: //if ledcolor equals 0 then the led1 will turn on
digitalWrite(led1, HIGH);
if (digitalRead(pb1),HIGH)
digitalWrite(led1,LOW);
break;
case 1: //if ledcolor equals 1 then the led2 will turn on
digitalWrite(led2, HIGH);
if (digitalRead(pb2),HIGH)
digitalWrite(led2,LOW);
break;
case 2: //if ledcolor equals 2 then the led3 will turn on
digitalWrite(led3, HIGH);
if (digitalRead(pb3),HIGH)
digitalWrite(led3,LOW);
break;
}
}
There are two timing problems in this program: 1) loop executes too quickly to see the response, 2) the pushbutton is read before the person has time to respond to the led.
I recommend restructuring your loop() to have the following general structure:
Turn on an led, based on random()
delay to give the person time to respond. For example, delay(500); will wait 1/2 second (1000 / 2).
read the pushbutton, based on a second switch(ledselect) code block.
delay to give the person time to get ready for the next loop. For example, delay(1000);
thankx alot I catch it ,i change the circuit so I Connect all output's switchs and connect to pin 4 in arduino (every switch become active only if his led on)
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb = 4;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0:
digitalWrite(led1, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led1,LOW);
break;
case 1:
digitalWrite(led2, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led2,LOW);
break;
case 2:
digitalWrite(led3, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led3,LOW);
break;
default:
delay(20);
break;
}
}

Arduino simple button activated light

I keep getting an error when I run this code any solutions?
{
pinMode(button2pin, INPUT, 3);
button1State= digitalRead(button1Pin 2);
}
if (button1State == LOW)
{
}
This code presumes that you have connected one side of your switch to digital pin 3 and the other side to ground and that your button is normally open.
const int button2pin = 3;
int button2State = 0;
void setup(){
pinMode(button2pin, INPUT_PULLUP);
Serial.begin(9600); // for debug only
}
void loop(){
button2State = digitalRead(button2pin);
if(button2State == LOW){
Serial.print("Button 2 pressed"); // for debug only
}
}
This project turns on the light when the button is pressed. Maybe can help you with your problem.
int buttonPin = 2;
int ledPin = 5;
//This var read the state of Buttonpin
int buttonState = 0;
void setup() {
// set the pin as output
pinMode(ledPin , OUTPUT);
// set the pin as input
pinMode(buttonPin , INPUT);
}
void loop(){
// read the button state
buttonState = digitalRead(buttonPin );
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}

Arduino Code LED failure to alternate

Traffic light just stays on red rather than alternating.
Wanted it to stay on for 10s then off for 10s, continuing ad infinitum.
Dont want to use the delay function cos need to do other stuff while the LED continues to alternate.
Thanks
int red = 10; // red traffic light LED on pin 10
int redcounter;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
redcounter = 0;
}
// the loop routine runs over and over again forever:
void loop()
{
redcounter = redcounter +1;
if(redcounter==1000)
{
redcounter=0;
if(digitalRead(red)==HIGH)
{
digitalWrite(red, LOW);
}
if(digitalRead(red)==LOW)
{
digitalWrite(red, HIGH);
}
}
You try to read a port which is configured as an OUTPUT. I don't know if this is supposed to work, but it would be more clear if you simply use another port as INPUT and feedback the signal you want to check in that port. I'm not sure however if it makes much sense to check the state of a signal you generate yourself (?). Moreover your redcounter is just "Active waiting", and arduino provides a delay function which does exactly that.
int red=10;
int signal=11;
void setup()
{
pinMode(red, OUTPUT);
pinMode(signal, INPUT);
digitalWrite(red, LOW);
}
void loop()
{
delay(1000);
if(digitalRead(signal)==HIGH)
{
digitalWrite(red, LOW);
}
if(digitalRead(signal)==LOW)
{
digitalWrite(red, HIGH);
}
}
Use elseif instead of if here:
if(digitalRead(red)==HIGH)
{
digitalWrite(red, LOW);
}
else if(digitalRead(red)==LOW)
{
digitalWrite(red, HIGH);
}
In your old solution every time red turned low, it was turned high a moment later.
Two issues in your code are that digitalread will not read an output pin and if you use an increment counter you won't be able to accurately denote time. Sorry if I missed a bracket or something I was doing this on the mobile app.
Use this:
int red = 10; // red traffic light LED on pin 10
int redcounter;
boolean pinState = false;
int delayTime = 10000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
redcounter = millis();
}
// the loop routine runs over and over again forever:
void loop() {
if((millis() - red counter) > delayTime) {
redcounter=millis();
if(pinState) {
digitalWrite(red, LOW);
pinState = false;
}
else {
digitalWrite(red, HIGH);
pinState = true;
}
}
}

Resources