Greetings,
I have sketch in tinkecad, loop()2 is not working. Also, I want relevant led to remain on till the second loop button is pressed. please help me out to correct my code...
#define LED_1 9 //Status of button_N remains on
#define LED_2 8 //Status of button_R remains on
#define BUTTON_N 0
#define BUTTON_R 1
#define BUTTON_1 2 //activate RL_1
#define BUTTON_2 3 //activate RL_2
#define RL_1 6
#define RL_2 7
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int buttonStateN = 0;
int buttonStateR = 0;
void setup()
{
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_R, INPUT);
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(RL_1, OUTPUT);
pinMode(RL_2, OUTPUT);
}
void loop1()
{
digitalWrite(LED_1, HIGH);
//digitalWrite(LED_2, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
}
void loop2()
{
digitalWrite(LED_2, HIGH);
//digitalWrite(LED_1, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
}
void loop()
{
buttonStateN = digitalRead(BUTTON_N);
buttonStateR = digitalRead(BUTTON_R);
if (buttonStateN == HIGH) {
loop1();
}
if (buttonStateR == HIGH) {
loop2();
}
}
Did you add in serial prints so you can see what's going on?
#define LED_1 9 //Status of button_N remains on
#define LED_2 8 //Status of button_R remains on
#define BUTTON_N 0
#define BUTTON_R 1
#define BUTTON_1 2 //activate RL_1
#define BUTTON_2 3 //activate RL_2
#define RL_1 6
#define RL_2 7
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int buttonStateN = 0;
int buttonStateR = 0;
void setup()
{
Serial.begin(115200);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_R, INPUT);
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(RL_1, OUTPUT);
pinMode(RL_2, OUTPUT);
}
void loop1()
{
digitalWrite(LED_1, HIGH);
//digitalWrite(LED_2, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
}
void loop2()
{
digitalWrite(LED_2, HIGH);
//digitalWrite(LED_1, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
}
void loop()
{
buttonStateN = digitalRead(BUTTON_N);
buttonStateR = digitalRead(BUTTON_R);
if (buttonStateN == HIGH) {
loop1();
}
Serial.print( "buttonStateR = ");
Serial.print( buttonStateR );
Serial.println();
if (buttonStateR == HIGH) {
loop2();
}
}
remove serial prints or comment out after finding out the deets and fixing the issue.
What do you mean by "not working" ?
Have you got resistors in place keeping inputs at a known state at al times ?
checked in serial monitor
buttonStateR = 0
buttonStateR =1
......................keeps running on.
I have shared tinkercad address for sketch for your kind perusal
In loop()1
relay RL_1 and RL_2 working well with switches.
In loop()2
relay RL_2 and RL_1 should work accordingly. but fails to do so.
Try using different pins for the input sensing pins on BUTTON_N and BUTTON_R. Pins 0 and 1 are used for serial communication.
1 Like
Strange After changing pins 0 & 1 to 2&3. motor not running
MCU type?
And always in a new post, post your lates code.
If you get a print out of buttonStateR =1
then the thing is already high.
Did you try adding in more serial prints?
#define LED_1 9 //Status of button_N remains on
#define LED_2 8 //Status of button_R remains on
#define BUTTON_N 0
#define BUTTON_R 1
#define BUTTON_1 2 //activate RL_1
#define BUTTON_2 3 //activate RL_2
#define RL_1 6
#define RL_2 7
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int buttonStateN = 0;
int buttonStateR = 0;
void setup()
{
Serial.begin(115200);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_R, INPUT);
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(RL_1, OUTPUT);
pinMode(RL_2, OUTPUT);
}
void loop1()
{
digitalWrite(LED_1, HIGH);
//digitalWrite(LED_2, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
}
void loop2()
{
digitalWrite(LED_2, HIGH);
//digitalWrite(LED_1, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
}
void loop()
{
buttonStateN = digitalRead(BUTTON_N);
buttonStateR = digitalRead(BUTTON_R);
if (buttonStateN == HIGH) {
loop1();
}
Serial.print( "buttonStateR = ");
Serial.print( buttonStateR );
Serial.println();
if (buttonStateR == HIGH) {
Serial.println( "woot woot");
loop2();
Serial.println( "woot woot qwertyuyuiop");
}
}
I see the diagram has changed on TinkerCad to using 2 and 3, but still set to 0 and 1 in code ? I dare say that BUTTON_1 and BUTTON_2 now need to be set to 3 and 4 as well since you have shifted them over to the next two pins? Unless of course you have a later version of your code?
I apologize if I can't elaborate my code,
Here again, I want BUTTON_N pressed it to start loop()1 or may be case statement.
in loop()1, BUTTON_1 and BUTTON_2 controls relay RL_1 & RL_2.
in loop()2, BUTTON_1 and BUTTON_2 controls relay RL_2 & RL_1.
only changes the rotation of motor
No, my apologies! I see that was (re)posted by idahowalker! I see that your code has been altered accordingly in your TinkerCad session.
When you changed 0 and 1 to 2 and 3, what did you change 2 and 3 to? You can't have two separate buttons on one pin.
#define LED_1 9 //Status of button_N remains on
#define LED_2 8 //Status of button_R remains on
#define BUTTON_N 2
#define BUTTON_R 3
#define BUTTON_1 4 //activate RL_1
#define BUTTON_2 5 //activate RL_2
#define RL_1 6
#define RL_2 7
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int buttonStateN = 0;
int buttonStateR = 0;
void setup()
{
Serial.begin(115200);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_R, INPUT);
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(RL_1, OUTPUT);
pinMode(RL_2, OUTPUT);
}
void loop1()
{
digitalWrite(LED_1, HIGH);
//digitalWrite(LED_2, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
}
void loop2()
{
digitalWrite(LED_2, HIGH);
//digitalWrite(LED_1, LOW);
BUTTONstate1 = digitalRead(BUTTON_1);
if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
BUTTONstate2 = digitalRead(BUTTON_2);
if (BUTTONstate2 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
}
void loop()
{
Serial.print( "buttonStateN = ");
Serial.print( buttonStateN );
Serial.println();
buttonStateN = digitalRead(BUTTON_N);
if (buttonStateN == LOW) {
loop1();
}
Serial.print( "buttonStateR = ");
Serial.print( buttonStateR );
Serial.println();
buttonStateR = digitalRead(BUTTON_R);
if (buttonStateR == HIGH) {
Serial.println( "woot woot");
loop2();
Serial.println( "woot woot qwertyuyuiop");
}
}
const int LED_1 = 9; //Status of button_N remains on
const int LED_2= 8; //Status of button_R remains on
const int BUTTON_N =2;
const int BUTTON_R =3;
const int BUTTON_1= 4; //activate RL_1
const int BUTTON_2= 5; //activate RL_2
const int RL_1 =6;
const int RL_2 =7;
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int buttonStateN = 0;
int buttonStateR = 0;
int lastPin1State,lastPin2State;
int lastN,lastR;
void setup()
{
Serial.begin(115200);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_R, INPUT);
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(RL_1, OUTPUT);
pinMode(RL_2, OUTPUT);
}
void loop1()
{
//digitalWrite(LED_1, HIGH);
//digitalWrite(LED_2, LOW);
//BUTTONstate1 = digitalRead(BUTTON_1);
int pinNState = digitalRead(BUTTON_1);
int pinRState = digitalRead(BUTTON_2);
if (pinNState == HIGH && lastN == LOW && pinRState == LOW)
// if (BUTTONstate1 == HIGH)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
//BUTTONstate2 = digitalRead(BUTTON_2);
//if (BUTTONstate2 == HIGH)
if (pinRState == HIGH && lastN == LOW && pinNState == LOW)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
}
void loop2()
{
//digitalWrite(LED_2, HIGH);
//digitalWrite(LED_1, LOW);
//BUTTONstate1 = digitalRead(BUTTON_1);
//if (BUTTONstate1 == HIGH)
int pinNState = digitalRead(BUTTON_1);
int pinRState = digitalRead(BUTTON_2);
if (pinNState == HIGH && lastN == LOW && pinRState == LOW)
{
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
//BUTTONstate2 = digitalRead(BUTTON_2);
//if (BUTTONstate2 == HIGH)
if (pinRState == HIGH && lastN == LOW && pinNState == LOW)
{
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
}
void loop()
{
Serial.print( "buttonStateN = ");
Serial.print( buttonStateN );
Serial.println();
int pin1State = digitalRead(BUTTON_N);
int pin2State = digitalRead(BUTTON_R);
if (pin1State == HIGH && lastPin1State == LOW && pin2State == LOW)
{
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
loop1();
}
Serial.print( "buttonStateR = ");
Serial.print( buttonStateR );
Serial.println();
if (pin2State == HIGH && lastPin2State == LOW && pin1State == LOW)
{
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, HIGH);
}
lastPin1State = pin1State;
lastPin2State = pin2State;
loop2();
}
Still loop()2 not running.
Do you mean that loop2() is never called or that when called it does not do what you want/expect ?
Yes Serial monitor shows fluctuating values.
Are you now saying that loop2() is actually running but not doing what you expect ? If so, then please explain what you expect it to do
const int LED_1 = 9; //Status of button_N remains on
const int LED_2= 8; //Status of button_R remains on
const int BUTTON_N =2;
const int BUTTON_R =3;
const int BUTTON_1= 4; //activate RL_1
const int BUTTON_2= 5; //activate RL_2
const int RL_1 =6;
const int RL_2 =7;
int buttonState1,buttonState2;
int buttonStateN,buttonStateR;
int lastPin1State,lastPin2State;
void setup()
{
Serial.begin(9600);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(BUTTON_N, INPUT);
pinMode(BUTTON_R, INPUT);
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(RL_1, OUTPUT);
pinMode(RL_2, OUTPUT);
}
void loop1(){
buttonState1 = digitalRead(BUTTON_1);
if (buttonState1 == LOW) {
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
buttonState2 = digitalRead(BUTTON_2);
if (buttonState2 == HIGH) {
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
}
void loop2(){
buttonState1 = digitalRead(BUTTON_1);
if (buttonState1 == LOW) {
digitalWrite(RL_2, HIGH);
}
else{
digitalWrite(RL_2, LOW);
}
buttonState2 = digitalRead(BUTTON_2);
if (buttonState2 == HIGH) {
digitalWrite(RL_1, HIGH);
}
else{
digitalWrite(RL_1, LOW);
}
}
void loop()
{
int buttonStaten = digitalRead(BUTTON_N);
Serial.println(buttonStaten);
int pin1State = digitalRead(BUTTON_N);
int pin2State = digitalRead(BUTTON_R);
if (pin1State == HIGH && lastPin1State == LOW && pin2State == LOW)
{
loop1();
delay(10);
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
}
int buttonStater = digitalRead(BUTTON_R);
Serial.println(buttonStater);
if (pin2State == HIGH && lastPin2State == LOW && pin1State == LOW)
{
loop2();
delay(10);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_1, LOW);
}
lastPin1State = pin1State;
lastPin2State = pin2State;
}
Now loop1 & loop 2 toggles but buttons not validating in either loop.