Pages: [1]   Go Down
Author Topic: Code timing issue?  (Read 655 times)
0 Members and 1 Guest are viewing this topic.
Pa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino Beginner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im having an issue with my code and the order that the way arduino runs.

In my code i setup several functions that activate Relays depending on the incoming condition. If i press button 1, the arduino activates one relay then after a delay activates another relay. If i press button 2, the same thing is sopposed to happen but on different relays.

Problem:
If button 2 gets pressed First, the delay in the function that gets activated for button 1 seems to be ignored. If button 1 is pressed first then button 2, everything seems to run fine.

Is there a better way to code this to operate correctly. :'(





Code:
//define Amp Error Inputs
int Button1 = 2;
int Button2 = 3;
int Button3 = 4;
int Button4 = 5;
int Button5 = 6;




//define Relay Outputs - Amp Switchover Relays
int BackupRelay1 = 23;
int BackupRelay2 = 22;
int MainRelay1 = 28;
int MainRelay2 = 27;
int MainRelay3 = 26;
int MainRelay4 = 25;
int MainRelay5 = 24;
int Main1RelayS = 29;
int Main2RelayS = 30;
int Main3RelayS = 31;
int Main4RelayS = 32;
int Main5RelayS = 33;


//define led pins - Indicators
int BackupLed1 = 8;
int BackupLed2 = 9;
int FullLed3 = 13;

//define total variable



int Amp1InError = 0;
int Amp2InError = 0;
int Amp3InError = 0;
int Amp4InError = 0;
int Amp5InError = 0;

int Backup1InUse;
int Backup2InUse;


void setup(){
  
  //turn on serial communication for debugging only
  Serial.begin(9600);
  
  
  //declare switch pins as INPUTS
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(Button3, INPUT);
  pinMode(Button4, INPUT);
  pinMode(Button5, INPUT);
  
  //declare pins as OUTPUT
  pinMode(BackupLed1, OUTPUT);
  pinMode(BackupLed2, OUTPUT);
  pinMode(FullLed3, OUTPUT);
  
  pinMode(BackupRelay1, OUTPUT);
  pinMode(BackupRelay2, OUTPUT);
  pinMode(MainRelay1, OUTPUT);
  pinMode(MainRelay2, OUTPUT);
  pinMode(MainRelay3, OUTPUT);
  pinMode(MainRelay4, OUTPUT);
  pinMode(MainRelay5, OUTPUT);
  pinMode(Main1RelayS, OUTPUT);
  pinMode(Main2RelayS, OUTPUT);
  pinMode(Main3RelayS, OUTPUT);
  pinMode(Main4RelayS, OUTPUT);
  pinMode(Main5RelayS, OUTPUT);
}


void loop(){
  Amp1InError = digitalRead(Button1);
  Amp2InError = digitalRead(Button2);
  Amp3InError = digitalRead(Button3);
  Amp4InError = digitalRead(Button4);
  Amp5InError = digitalRead(Button5);
  
  
  Backup1InUse = 0;
  Backup2InUse = 0;
  
  
  if(Amp1InError == HIGH) //If amp 1 is in error
  {
    if(Backup1InUse == 1)
    {
      if(Backup2InUse == 1)
      {
      }
      if(Backup2InUse == 0)
      {
        Backup2InUse = 1;
        Main1PathB();
      }
    }
    if(Backup1InUse == 0)
    {
      Backup1InUse = 1;
      Main1PathA();
    }
  }
  
  
  
  if(Amp2InError == HIGH) //If amp 2 is in error
  {
    if(Backup1InUse == 1)
    {
      if(Backup2InUse == 1)
      {
      }
      if(Backup2InUse == 0)
      {
        Backup2InUse = 1;
        Main2PathB();
      }
    }
    if(Backup1InUse == 0)
    {
      Backup1InUse = 1;
      Main2PathA();
    }
  }
  
  
  
  
}






//AMP 1
void Main1PathA()
{
digitalWrite(Main1RelayS, LOW);  //path A
digitalWrite(BackupRelay1, HIGH);//turn on backup A
Backup1InUse = 1;
delay (250);
digitalWrite(MainRelay1, HIGH);  //switch from Main amp to Backup amp
}
void Main1PathB()
{
digitalWrite(Main1RelayS, HIGH);  //path B
digitalWrite(BackupRelay2, HIGH);  //turn on backup B
Backup2InUse = 1;
delay (250);
digitalWrite(MainRelay1, HIGH);  //switch from Main amp to Backup amp
}


//AMP 2
void Main2PathA()
{
digitalWrite(Main2RelayS, LOW);  //path A
digitalWrite(BackupRelay1, HIGH);  //turn on backup A
Backup1InUse = 1;
delay (250);
digitalWrite(MainRelay2, HIGH);  //switch from Main amp to Backup amp
}
void Main2PathB()
{
digitalWrite(Main2RelayS, HIGH);  //path B
digitalWrite(BackupRelay2, HIGH);  //turn on backup B
Backup2InUse = 1;
delay (250);
digitalWrite(MainRelay2, HIGH);  //switch from Main amp to Backup amp
}


//AMP 3
void Main3PathA()
{
digitalWrite(Main3RelayS, LOW);  //path A
digitalWrite(BackupRelay1, HIGH);  //turn on backup A
Backup1InUse = 1;
delay (250);
digitalWrite(MainRelay3, HIGH);  //switch from Main amp to Backup amp
}
void Main3PathB()
{
digitalWrite(Main3RelayS, HIGH);  //path B
digitalWrite(BackupRelay2, HIGH);  //turn on backup B
Backup2InUse = 1;
delay (250);
digitalWrite(MainRelay3, HIGH);  //switch from Main amp to Backup amp
}


//AMP 4
void Main4PathA()
{
digitalWrite(Main4RelayS, LOW);  //path A
digitalWrite(BackupRelay1, HIGH);  //turn on backup A
Backup1InUse = 1;
delay (250);
digitalWrite(MainRelay4, HIGH);  //switch from Main amp to Backup amp
}
void Main4PathB()
{
digitalWrite(Main4RelayS, HIGH);  //path B
digitalWrite(BackupRelay2, HIGH);  //turn on backup B
Backup2InUse = 1;
delay (250);
digitalWrite(MainRelay4, HIGH);  //switch from Main amp to Backup amp
}


//AMP 5
void Main5PathA()
{
digitalWrite(Main5RelayS, LOW);  //path A
digitalWrite(BackupRelay1, HIGH);  //turn on backup A
Backup2InUse = 1;
delay (250);
digitalWrite(MainRelay5, HIGH);  //switch from Main amp to Backup amp
}
void Main5PathB()
{
digitalWrite(Main5RelayS, HIGH);  //path B
digitalWrite(BackupRelay2, HIGH);  //turn on backup B
Backup2InUse = 1;
delay (250);
digitalWrite(MainRelay5, HIGH);  //switch from Main amp to Backup amp
}








Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use Serial.print() to show the name(s) of the functions being executed (except loop()).

Perhaps there is a flaw in you logic that you (and I) can't see.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps some arrays could make the logic simpler?
Logged

Per Arduino ad Astra

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
loop() {
...
  Backup1InUse = 0;
  Backup2InUse = 0;
...

After those lines, all test for BackupXInUse will yield the same results. I guess, you should initialise them in the setup() function or initialise those variables to the correct value before testing for them.

Korman
Logged

Pa, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino Beginner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
After those lines, all test for BackupXInUse will yield the same results. I guess, you should initialise them in the setup() function or initialise those variables to the correct value before testing for them.

Korman



If i remove them, when the loop comes back around, both Main1PathA and Main1PathB are active at the same time, which cant happen.

And after testing, if i have buttons 1 and 2 on and reset the arduino, the timing is correct. :-?

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25813
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could tidy up your code a bit:
Code:
if(Backup1InUse == 1)
    {
      if(Backup2InUse == 1)
      {
      }
If you haven't got anything to do when "Backup2InUse == 1", don't put anything.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: