loop1 & loop2 not running by pressing relevant buttons

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
virencq
 
Posts: 1
Joined: Mon Jan 09, 2023 9:28 am

loop1 & loop2 not running by pressing relevant buttons

Post by virencq »

Greetings,
I have sketch in tinkecad (Login | Tinkercad 2), please help me out to correct my code...

Code: Select all

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;
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 && lastR == LOW && pinNState == LOW) 
  {
    digitalWrite(RL_2, HIGH);
  } 
  else{
    digitalWrite(RL_2, LOW);
  }
  lastN = pinNState;
  lastR = pinRState;
   delay(10);
}

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 && lastR == LOW && pinNState == LOW) 
  {
    digitalWrite(RL_1, HIGH);
  } 
  else{
    digitalWrite(RL_1, LOW);
  }
   lastN = pinNState;
   lastR = pinRState;
   delay(10);
  
}


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) 
  {
    
      
      loop1();
  } 
  Serial.print( "buttonStateR = ");
  Serial.print( buttonStateR );
  Serial.println();
    if (pin2State == HIGH && lastPin2State == LOW && pin1State == LOW) 
  {
      loop2();
    
  }
  lastPin1State = pin1State;
  lastPin2State = pin2State;
   
      
    }

User avatar
adafruit_support_bill
 
Posts: 88099
Joined: Sat Feb 07, 2009 10:11 am

Re: loop1 & loop2 not running by pressing relevant buttons

Post by adafruit_support_bill »

How do you have your buttons wired? Do you have pullup or pulldown resistors on them?

https://learn.adafruit.com/adafruit-ard ... s/overview

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”