Go Down

Topic: Why does my Arduino UNO crash with the below Sketch.... (Read 2066 times) previous topic - next topic

GlennP

Hi All,

Bit of a nerve but I am coding a buggy controller I can get the forward, left, right, and back action needed from the below sketch but the Arduino Uno I am using gets hung up, enters a loop which I can't get out of with the below sketch (I know its not elegant but its a proof of concept)

 
Robot 1a   7/1/2014 10:53, 
 Code to get Switching of LED switch LED's See Photo 
 Test for driving H-Bridge 13/03/2014
 Getting Second set of switches to switch 15/3/2014 16:24
 Got H-Bridge soldered up and tested connection to Uno,
 sucessfully, next work on case! 29/03/2014 18:44`
 
 Now getting Uno to Drive Motors Sensibly 24/12/2014
 Getting responce to switches better needs more testing 27/12/2014
 Getting Left & Right Turns & Backwards control 29/12/2014
 Suddenly working further test tomorrow! 29/12/2014
 
 
 */
const int LEDPin = 13;
const int LEDPin1 = 7;
const int LEDPin2 = 8;
const int LEDPin3 = 9;
const int LEDPin4 = 10;
const int InputPin1 = 2;
const int InputPin2 = 4;
const int InputPin3 = 3;
const int InputPin4 = 5;
int delayTime = 100;


void setup()
{
  pinMode(LEDPin1, OUTPUT);
  pinMode(LEDPin, OUTPUT);
  pinMode(LEDPin2, OUTPUT);
  pinMode(LEDPin3, OUTPUT);
  pinMode(LEDPin4, OUTPUT);

  pinMode(InputPin1, INPUT);
  pinMode(InputPin2, INPUT);
  pinMode(InputPin3, INPUT);
  pinMode(InputPin4, INPUT);


  digitalWrite(InputPin1, HIGH);
  digitalWrite(InputPin2, HIGH);
  digitalWrite(InputPin3, HIGH);
  digitalWrite(InputPin4, HIGH);
}



void loop()
{
  Serial.begin(9600);
  digitalWrite(LEDPin,HIGH);
  int val1 = digitalRead(InputPin1);
  int val2 = digitalRead(InputPin2);
  int val3 = digitalRead(InputPin3);
  int val4 = digitalRead(InputPin4);
  int a;

if(val1 == HIGH && val3 == HIGH)
{
  for(a = 0; a<=10; a++)
  {
    digitalWrite(LEDPin1,HIGH);
    digitalWrite(LEDPin3,HIGH);
    Serial.println(" Forwards!");
    delay(delayTime);
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
  }
}
if(val2 == LOW)
{
  for(a = 0;a<=100;a++)
  {
    Serial.println("RESET");
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
    delay(delayTime);
  }
}

//will drive forwards until switch hit, need to find way of getting turn and then resetting to drive
//might have it, needs more testing & tuning of for loop
/* 
  {
  //Serial.println(" Forwards Called ");
 
  }else
  while(val1 == HIGH && val3 == HIGH)
  {
   
  }
  */
  if(val1 == LOW && val3 == HIGH)
  {
    for(a = 0; a<=10; a++)
   {
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
    delay(delayTime);   
    digitalWrite(LEDPin1,HIGH);
    digitalWrite(LEDPin3,LOW);
     Serial.println(" Turn Left!");
    delay(delayTime);
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
   }
 }
 if(val1 == HIGH && val3 == LOW)
 {
   for(a = 0; a<=10; a++)
   {
     digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
    delay(delayTime);   
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,HIGH);
    Serial.println(" Turn Right!");
    delay(delayTime);
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
   }
 }
 if(val1 == LOW && val3 == LOW)
 {
   for(a = 0; a <=10;a++)
    {
    Serial.println("STOP!!");
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
    //digitalWrite(LEDPin1,LOW);
    //digitalWrite(LEDPin3,LOW);
    }
   for(a = 0; a <=4 ; a++)
   {
     delay(delayTime);
     Backwards();
   }
 }

}
*/
void Backwards()
{
 
  for(int a = 0; a <= 10; a++)
  {
  //both motors Backwards
  digitalWrite(LEDPin2,HIGH);
  digitalWrite(LEDPin4,HIGH);
  Serial.println(" Backwards! ");
 }
}

JimboZA

You've got unbalanced code tags in there so it's impossible to tell what's supposed to be there or not. Fix that and put it all in code tags so we can read it...

Better yet, actually remove any code that's not supposed to be there.

Also explain what you mean by "enters a loop which I can't get out of ". I'm assuming you don't mean the...

Code: [Select]
void loop()
{
}


...loop?
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

mrburnette

#2
Jan 02, 2015, 01:46 pm Last Edit: Jan 02, 2015, 01:48 pm by mrburnette
To expand on what JimboZA has stated, simply use Tools / Auto Format and then copy the entire code into the Reply block but with codetags at the top/bottom so the HTML formatter will manage the cut&paste properly.

You can type the codetags:  
Code: [Select]
 <your stuff>
 or do the lazy thing and click the codetag icon on the top toolbar of the Reply editor... the 7th icon from the right edge on the top line... looks like a scroll with "<>" in the graphic.

Not only does it help us to visualize the code logic areas, but it helps you by allowing quick identification of problem areas.

Snippet of your code:
Code: [Select]
  if(val1 == LOW && val3 == HIGH)
  {
    for(a = 0; a<=10; a++)
   {
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
    delay(delayTime);   
    digitalWrite(LEDPin1,HIGH);
    digitalWrite(LEDPin3,LOW);
     Serial.println(" Turn Left!");
    delay(delayTime);
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
   }
 }


Looks better, ugh?

Thanks,

Ray

GlennP

Hi,
Thanks for that as I said I'm new to the board, will reformat my code and repost it.  Also the loop seems to after it has done the reverse part (Backwards) it triggers a loop inside Loop which just prints Forwards over and over and causes the Uno to produce a clicking sound.  I am having a little trouble getting the source to appear nicely formatted as it is on a test PC running Win XP and I am typing this on a Windows 8 machine.

oric_dan

The first thing to do when writing code is to modularize it in a hierarchical fashion, and give the modules nice functional names. Logic at the top, details down below. Then you can read what it's supposed to be doing without getting bogged down in the details of how it's doing it. And you can fix the low-level modules without getting bogged down in how they fit together. And you can more easily see logical errors. Win-win-win.

So step 1:
Code: [Select]
void loop()
{
....
  if( val1 == LOW && val3 == HIGH) go_forward();
  if(val2 == LOW) reset();
....
  if(val1 == LOW && val3 == HIGH) turn_left();
....
}

void go_forward()
{
  for(a = 0; a<=10; a++)
  {
    digitalWrite(LEDPin1,HIGH);
    digitalWrite(LEDPin3,HIGH);
    Serial.println(" Forwards!");
    delay(delayTime);
    digitalWrite(LEDPin1,LOW);
    digitalWrite(LEDPin3,LOW);
  }
}

Better yet ---> other people can read it, and more easily guess what it's supposed to be doing.

Go Up