Pages: 1 [2]   Go Down
Author Topic: Control flow troubles  (Read 1409 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you gents!
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: February 04, 2013, 03:18:35 pm by AWOL » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 638
Posts: 50307
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Where did I go wrong?
I think it started with the copy/paste of your code. Somehow, you mangled that pretty badly.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: 1 [2]   Go Up
Jump to: