Go Down

Topic: Problem with setting a bool inside of an if loop (Read 231 times) previous topic - next topic

jhartt3

Dec 16, 2017, 12:50 pm Last Edit: Dec 16, 2017, 01:02 pm by jhartt3
So i'm trying to set the state of and actuator "SurfLeftDeployed" i'm setting it to false to start the code but in my loop at the bottom when i trigger the input it just keeps cycling thru the same sequence when the input is high and when the input(SurfLeft) is low.  Am i not setting the state of SurfLeftDeployed properly.  



thanks in advance.  
Code: [Select]
////variable declaration

// selection pins on which are connected to the Selector
//int pinsSelectorPosAuto = 28;
//int pinsSelectorPosManu = 29;
const int pinSurfRight = 4;
const int pinSurfLeft = 5;
const int RelayGateRight = 6;
const int RelayGateRetRight = 7;
const int RelayGateLeft = 8;
const int RelayGateRetLeft = 9;
//int relayAlimFeuStar = 33;
//int relayAlimFiresUnderMarin = 34;
//int pinsremotecontrolLight = 35;
//int pinsMaliviewLightTour = 36;
//int pinsMaliviewLightUnderMarin = 37;
//int pinsremotecontrolSurfgate = 39;  // probably a wiring error when the 2 inputs are inverted so that it matches the good buttons on the remotecontrol.
//int pinsremotecontrolCoteSurfgate = 38;  // probably a wiring error when the 2 inputs are inverted so that it matches the good buttons on the remotecontrol.
int SurfRight = 0;
int SurfLeft = 0;
int SurfLeftOn = 0;
int SurfRigthOn = 0;
int SurfLeftDeploy = 0;
int SurfRightDeploy = 0;
bool SurfLeftDeployed = false;
//const int SurfRightDeployed = 11;
int speedhigh = 0;
int speedlow = 0;

void setup() {
  gpsSerial.begin(9600);
  delay(1000);
  pinMode(pinSurfRight, INPUT);
  pinMode(pinSurfLeft, INPUT);
  pinMode(RelayGateRight, OUTPUT);
  pinMode(RelayGateRetRight, OUTPUT);
  pinMode(RelayGateLeft, OUTPUT);
  pinMode(RelayGateRetLeft, OUTPUT);
  //pinMode(SurfLeftDeployed, OUTPUT);
  //pinMode(SurfRightDeployed, OUTPUT);

}

void displayGPS( gps_fix & fix )
{
  int mph = 0;
  int speedhigh = 0;
  int speedlow = 0;
 
  // Print speed
  if (fix.valid.speed && (fix.spd.whole > 5)) {
    mph = (fix.spd.whole * 115) / 100;
  }
  Serial.print( mph );
  Serial.print( (" mph      ") );

  if (mph < 15)
    speedhigh = 1;
  else
    speedhigh = 0;
   
  if (mph > 7)
    speedlow = 1;
  else
    speedlow = 0;
   

 
} // displayGPS


void loop()
    {
  SurfRight=digitalRead(pinSurfRight);
  SurfLeft=digitalRead(pinSurfLeft);
 
  /*while (gps.available( gpsSerial )) {  // if there is data coming from the GPS shield
    gps_fix fix = gps.read();           // get the complete fix structure
    displayGPS( fix );                  // Show pieces of the fix on Serial
  }*/
  //SURF LEFT LOGIC
  //if (speedhigh = true) && (speedlow == true)
  if (SurfLeft == HIGH)
  {
    if (SurfLeftDeployed == false);{
    digitalWrite(RelayGateRight, HIGH);
    delay(3000);
    digitalWrite(RelayGateRight, LOW);
    SurfLeftDeployed = true;}
   
  }
   if (SurfLeft == LOW)// or speedhigh == 0 or speedlow == 0)
    {
    if(SurfLeftDeployed == true);
      {digitalWrite(RelayGateRight, HIGH);
      digitalWrite(RelayGateRetRight, HIGH);
      delay(3000);
      digitalWrite(RelayGateRight, LOW);
      digitalWrite(RelayGateRetRight, LOW);
      SurfLeftDeployed = false;}}

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

jhartt3


UKHeliBob

#3
Dec 16, 2017, 01:18 pm Last Edit: Dec 16, 2017, 01:20 pm by UKHeliBob
Code: [Select]
    if (SurfLeftDeployed == false);I see a semicolon that should not be there and several more too.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jhartt3

removed that still not appearing to work properly

jhartt3

so now the issue appears to be that it constantly cycles thru both of the bottom rungs when SURFLEFT is off when SURFLEFT is on it deploys and stops after 3 seconds but if i take power off the pin it just constantly moves in for 3 seconds then out for 3 seconds.

jhartt3

Code: [Select]
 if (SurfLeft == HIGH) {
  if (SurfLeftDeployed == false){
    digitalWrite(RelayGateRight, HIGH);
    delay(3000);
    digitalWrite(RelayGateRight, LOW);
    SurfLeftDeployed = true;}
    
  }
     if (SurfLeft == LOW){
     if (SurfLeftDeployed == true) {
      digitalWrite(RelayGateRight, HIGH);
      digitalWrite(RelayGateRetRight, HIGH);
      delay(3000);
      digitalWrite(RelayGateRight, LOW);
      digitalWrite(RelayGateRetRight, LOW);
      SurfLeftDeployed = false;}}


new logic at the bottom

jhartt3

do i need to debounce the input is it likely flickering when low.  and whats the easiest way to implement that

UKHeliBob

Please Auto Format the whole of the program as it is now and post it here.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jhartt3

Code: [Select]

////variable declaration

// selection pins on which are connected to the Selector
//int pinsSelectorPosAuto = 28;
//int pinsSelectorPosManu = 29;
const int pinSurfRight = 4;
const int pinSurfLeft = 5;
const int RelayGateRight = 6;
const int RelayGateRetRight = 7;
const int RelayGateLeft = 8;
const int RelayGateRetLeft = 9;
//int relayAlimFeuStar = 33;
//int relayAlimFiresUnderMarin = 34;
//int pinsremotecontrolLight = 35;
//int pinsMaliviewLightTour = 36;
//int pinsMaliviewLightUnderMarin = 37;
//int pinsremotecontrolSurfgate = 39;  // probably a wiring error when the 2 inputs are inverted so that it matches the good buttons on the remotecontrol.
//int pinsremotecontrolCoteSurfgate = 38;  // probably a wiring error when the 2 inputs are inverted so that it matches the good buttons on the remotecontrol.
int SurfRight = 0;
int SurfLeft = 0;
int SurfLeftOn = 0;
int SurfRigthOn = 0;
int SurfLeftDeploy = 0;
int SurfRightDeploy = 0;
bool SurfLeftDeployed = false;
//const int SurfRightDeployed = 11;
int speedhigh = 0;
int speedlow = 0;

void setup() {
  gpsSerial.begin(9600);
  delay(1000);
  pinMode(pinSurfRight, INPUT);
  pinMode(pinSurfLeft, INPUT);
  pinMode(RelayGateRight, OUTPUT);
  pinMode(RelayGateRetRight, OUTPUT);
  pinMode(RelayGateLeft, OUTPUT);
  pinMode(RelayGateRetLeft, OUTPUT);
  //pinMode(SurfLeftDeployed, OUTPUT);
  //pinMode(SurfRightDeployed, OUTPUT);

}

void displayGPS( gps_fix & fix )
{
  int mph = 0;
  int speedhigh = 0;
  int speedlow = 0;
 
  // Print speed
  if (fix.valid.speed && (fix.spd.whole > 5)) {
    mph = (fix.spd.whole * 115) / 100;
  }
  Serial.print( mph );
  Serial.print( (" mph      ") );

  if (mph < 15)
    speedhigh = 1;
  else
    speedhigh = 0;
   
  if (mph > 7)
    speedlow = 1;
  else
    speedlow = 0;
   

 
} // displayGPS


void loop()
    {
  SurfRight=digitalRead(pinSurfRight);
  SurfLeft=digitalRead(pinSurfLeft);
 
  /*while (gps.available( gpsSerial )) {  // if there is data coming from the GPS shield
    gps_fix fix = gps.read();           // get the complete fix structure
    displayGPS( fix );                  // Show pieces of the fix on Serial
  }*/
  //SURF LEFT LOGIC
  //if (speedhigh = true) && (speedlow == true)

  if (SurfLeft == HIGH){
  if (SurfLeftDeployed == false){
    digitalWrite(RelayGateRight, HIGH);
    delay(4000);
    digitalWrite(RelayGateRight, LOW);
    SurfLeftDeployed = true;}
   
  }
     if (SurfLeft == LOW) {
     if (SurfLeftDeployed == true) {
      digitalWrite(RelayGateRight, HIGH);
      digitalWrite(RelayGateRetRight, HIGH);
      delay(3000);
      digitalWrite(RelayGateRight, LOW);
      digitalWrite(RelayGateRetRight, LOW);
      SurfLeftDeployed = false;}}

jhartt3

so i just did a test and swapped the low and high in the if statements.  and it acts the same way if there is no power on pin 5 it cycles in and out like there is noise on it or something.  Is there an easy way to debounce it when its low or to make sure its really 0 or make sure its fully 5v before arduino does a step.

jhartt3

inverted the input to pull it up and will just ground it to turn it on and it now functions properly.  Likely was noise on the input when "low" is all i can come up with thoughts?

Code: [Select]

////variable declaration

// selection pins on which are connected to the Selector
//int pinsSelectorPosAuto = 28;
//int pinsSelectorPosManu = 29;
const int pinSurfRight = 4;
const int pinSurfLeft = 5;
const int RelayGateRight = 6;
const int RelayGateRetRight = 7;
const int RelayGateLeft = 8;
const int RelayGateRetLeft = 9;
//int relayAlimFeuStar = 33;
//int relayAlimFiresUnderMarin = 34;
//int pinsremotecontrolLight = 35;
//int pinsMaliviewLightTour = 36;
//int pinsMaliviewLightUnderMarin = 37;
//int pinsremotecontrolSurfgate = 39;  // probably a wiring error when the 2 inputs are inverted so that it matches the good buttons on the remotecontrol.
//int pinsremotecontrolCoteSurfgate = 38;  // probably a wiring error when the 2 inputs are inverted so that it matches the good buttons on the remotecontrol.
int SurfRight = 0;
int SurfLeft = 0;
int SurfLeftOn = 0;
int SurfRigthOn = 0;
int SurfLeftDeploy = 0;
int SurfRightDeploy = 0;
bool SurfLeftDeployed = false;
//const int SurfRightDeployed = 11;
int speedhigh = 0;
int speedlow = 0;

void setup() {
  gpsSerial.begin(9600);
  delay(1000);
  pinMode(pinSurfRight, INPUT);
  pinMode(pinSurfLeft, INPUT_PULLUP);
  pinMode(RelayGateRight, OUTPUT);
  pinMode(RelayGateRetRight, OUTPUT);
  pinMode(RelayGateLeft, OUTPUT);
  pinMode(RelayGateRetLeft, OUTPUT);
  //pinMode(SurfLeftDeployed, OUTPUT);
  //pinMode(SurfRightDeployed, OUTPUT);

}

void displayGPS( gps_fix & fix )
{
  int mph = 0;
  int speedhigh = 0;
  int speedlow = 0;
 
  // Print speed
  if (fix.valid.speed && (fix.spd.whole > 5)) {
    mph = (fix.spd.whole * 115) / 100;
  }
  Serial.print( mph );
  Serial.print( (" mph      ") );

  if (mph < 15)
    speedhigh = 1;
  else
    speedhigh = 0;
   
  if (mph > 7)
    speedlow = 1;
  else
    speedlow = 0;
   

 
} // displayGPS


void loop()
    {
  SurfRight=digitalRead(pinSurfRight);
  SurfLeft=digitalRead(pinSurfLeft);
 
 
  /*while (gps.available( gpsSerial )) {  // if there is data coming from the GPS shield
    gps_fix fix = gps.read();           // get the complete fix structure
    displayGPS( fix );                  // Show pieces of the fix on Serial
  }*/
  //SURF LEFT LOGIC
  //if (speedhigh = true) && (speedlow == true)

  if (SurfLeft == LOW){
  if (SurfLeftDeployed == false){
    digitalWrite(RelayGateRight, HIGH);
    delay(4000);
    digitalWrite(RelayGateRight, LOW);
    SurfLeftDeployed = true;}
   
  }
     if (SurfLeft == HIGH) {
     if (SurfLeftDeployed == true) {
      digitalWrite(RelayGateRight, HIGH);
      digitalWrite(RelayGateRetRight, HIGH);
      delay(4500);
      digitalWrite(RelayGateRight, LOW);
      digitalWrite(RelayGateRetRight, LOW);
      SurfLeftDeployed = false;}}

 

jhartt3

now to get the GPS portion of this working!

UKHeliBob

Quote
Likely was noise on the input when "low" is all i can come up with thoughts?
If you did not have a pulldown resistor in place to keep the input at a known state then all bets are off as it floats at an uncertain voltage.

Code: [Select]
  pinMode(pinSurfRight, INPUT);
  pinMode(pinSurfLeft, INPUT_PULLUP);
Why are the pinMode()s of these two pins different from one another ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jhartt3

they were different b/c i was just testing using the left side ... they are both the same now.  thanks for your help it works amazingly well just took it for a spin the car to test the speed deployment with GPS and it works great.  no issues anymore now that i'm pulling that up to 5V and then grounding it for the input.

Go Up