Go Down

Topic: Control flow troubles (Read 1 time) previous topic - next topic

alkopop79

I do appreciate your effort and promise next time I paste my code I'll autoformat it beforehand.

billroy

The reason this gets harped on is that mistakes that jump right out of code that is properly indented can hide for days if entropic indentation is used.

It really is worth taking the time: fortune favors those who keep the code properly indented at all times.  It doesn't matter if you put the { on a separate line or not (I don't, PaulS does, it's a matter of taste), but I guess that this "underhanging {" style is required for auto-format.  I just keep it correct while editing.  It's easy enough.

-br

PeterH


I do use Auto Format it's just I can't stand the bracket taking up an extra line. Never thought it can be a problem.


Back in the days when programming was done on a 25-line teletype, that was a valid concern. These days with huge desktops everywhere the cost of one extra line in a file is insignificant compared to the advantages of having the { and } pairs lined up vertically so that you can pick out the matching pairs easily so that the actual control structure is obvious. Laying your code out well is one of the most important ways you can make it clear what it does, both for you and for anyone else who has to look at it.
I only provide help via the forum - please do not contact me for private consultancy.

alkopop79


alkopop79

#19
Feb 04, 2013, 09:00 pm Last Edit: Feb 04, 2013, 09:18 pm by AWOL Reason: 1
This time a different code gives me headache. I'm trying to control two DC motors from the serial. The idea is that the direction and the speed of the two motors is determined by commands starting with the character 'l' (left) or 'r' (right). The character is followed then by a numeric value. A function called 'parse' creates a string and then turns is into an int. However the console tells me otherwise... somehow I got lost.

Quote

char inData[10];
int index;
boolean started = false;
boolean ended = false;


void setup()
{
 Serial.begin(9600);
 Serial.println("**************READY****************");
}

void loop()
{
 while(Serial.available() > 0)
 {
   int l=parse('l');
   int r=parse('r');
   drive(5,6,'l','L' );
   drive(9,10,'r','R');
   // Serial.println(Serial.read());
 }

}

int parse(char id){
 char aChar = Serial.read();
 if(aChar == id)
 {
   started = true;
   index = 0;
   inData[index] = '\0';
 }
 else if(aChar == '\n')
 {
   ended = true;
 }
 else if(started)
 {
   inData[index] = aChar;
   index++;
   inData[index] = '\0';

 }

 if(started && ended)
 {
   int inInt = atoi(inData); // Convert the string to an integer
   return inInt;
   // Get ready for the next time
   started = false;
   ended = false;
   index = 0;
   inData[index] = '\0';
 }
}
void drive(int p1,int p2, int dir, char eyed)
{
 if(dir>0){
   analogWrite(p1,dir);
   analogWrite(p2,0);
    Serial.print(eyed);
   Serial.print(" ");;
   Serial.println('f');
 }
 else if(dir<0)
 {
   analogWrite(p1,0);
   analogWrite(p2,dir);
   Serial.print(eyed);
   Serial.print(" ");
   Serial.println('b');
 }
 else if(dir==0)
 {
   analogWrite(p1,0);
   analogWrite(p2,0);
   Serial.print(eyed);
   Serial.print(" ");
   Serial.println('n');
 }
}













Where did I go wrong?

PaulS

Quote
Where did I go wrong?

I think it started with the copy/paste of your code. Somehow, you mangled that pretty badly.

PeterH

I think your approach to parsing is a complete non-starter.

I suggest that you decide to include a newline at the end of each command, and add code which buffers the received text until it reaches the newline and then parses what is in the buffer. It only need a couple of lines of code to decide whether the first character is 'l' or 'r' and call atoi() to find the following number.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up