Pages: [1]   Go Down
Author Topic: Strange delay in Serial until code starts  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I decided to alter some code I found online for driving servos to something more user friendly. The initial code dealt with a char array hardcoded into the sketch. For every 'f' the servos would go forward for 200ms, for every 'b' backwards for 200ms, for every 'l' left for 200ms, for every 'r' right for 200ms, and for 's' just stop completely. I decided to recode this so that it drives based on user input to Serial. This current code works, however there is a strange delay I found. I can send any string of characters I want into Serial, and it will perform as expected, but only after waiting around 10-15 seconds. The code just does nothing for 10-15 seconds in the beginning when you send a character to Serial, but then it works perfectly. Then after that, I don't think it will do anything if you try to send more characters a second time (probably has to do with 's' stopping it, but that is my second bug to handle). Anyone see anything strange in the code that could cause for this? Here is my current code:

Code:
#include <Servo.h>   //include servo library

Servo servoLeft;   //declare servo objects
Servo servoRight;

void setup() {
 
  servoLeft.attach(13);   //attach left servo to pin 13
  servoRight.attach(12);   //attach right servo to pin 12
 
  Serial.begin(9600);   //begin Serial at 9600 baud
 
  char maneuvers[] = {Serial.read()};   //read incoming characters and write to maneuvers[] array
 
  int index = 0;   //initialize index as int at 0
 
  do {
 
    go(maneuvers[index]);   //use go() function each time through index of maneuvers[] while 's' is not read in the index of maneuvers[]
 
  } while (maneuvers[index++] != 's');   
 
  }
 
}

void loop() {
}

void go(char c) {   //go() takes 1 parameter of a char

  switch(c) {
 
    case 'f':
      servoLeft.writeMicroseconds(1700);   //in the case of 'f' drive both servos forward
      servoRight.writeMicroseconds(1300);
      break;
    case 'b':
      servoLeft.writeMicroseconds(1300);   //in the case of 'b' drive both servos backward
      servoRight.writeMicroseconds(1700);
      break;
    case 'l':
      servoLeft.writeMicroseconds(1300);   //in the case of 'l' drive servos left
      servoRight.writeMicroseconds(1300);
      break;
    case 'r':
      servoLeft.writeMicroseconds(1700);   //in the case of 'r' drive servos right
      servoRight.writeMicroseconds(1700);
      break;
    case 's':
      servoLeft.writeMicroseconds(1500);   //in the case of 's' stop both servos
      servoRight.writeMicroseconds(1500);
      break;
 
  }
 
  delay(200);   //do everything for 200ms after break in case
 
}
Logged

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

Quote
however there is a strange delay
I found it.
Code:
delay(200);


Code:
char maneuvers[] = {Serial.read()};
I don't really expect that to work.
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.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(probably has to do with 's' stopping it, but that is my second bug to handle).
Bug? All your code is in setup(). Once it receives an 's', it returns from setup() and enters loop(), which you have defines as

Code:
void loop() {
}

Which means, don't do anything. And keep not doing anything over and over again.
Logged

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

Required reading
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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4019
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone see anything strange in the code that could cause for this?

Like the unbalanced braces in setup that keep it from compiling? Yes, strange that it runs for you!
Logged

Examples can be found in your IDE.

Pages: [1]   Go Up
Jump to: