Robot command without delay

I would like my computer to control a robot i am working on. there are seven commands, once they are finished they return the number of their command. Additionally, one command returns a value from my robot's ping sensor. The old version of this code had delays, but i am working to re-write it without any. This code compiles and uploads, but does not function correctly.

// 0 = move forward; 1 = turn right; 2 = turn left; 3 = u turn; 4 = turn head forward; 5 = turn head right; 6 = turn head left; 7 = take reading

#include <Servo.h>

// contains all the delay times for different servos and moving directions. can be referenced by code number for each action
// moveTime, rTurn, lTurn, hR, hL, hS
const int times[] = {2240/10, 620/10, 500/10, 620 * 2/10, 500/10, 500/10, 500/10};

const byte lHead = 4; // left head angle
const byte rHead = 176; // right head angle
const byte fHead = 90; // middle head angle
const byte pingPin = 7; // Ping))) sensor pin number
Servo lServo, rServo, Neck; // the servos being used
const byte lZero = 94; // the zero value for one cont. rotation servo
const byte rZero = 95; // the zero value for the other cont. rotation servo

// variables for non delay() scheme
int current = millis();
int old = 0;
byte exe = -1;
int numTimes = 0;

// currently functionized, stolen from example code provided with the IDE
long ping() {
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
long duration = pulseIn(pingPin, HIGH); // get length
long inches = duration / 74 / 2; // get distance
return inches;
}

void setup() {
// start everything and zero motors
Serial.begin(9600);
lServo.attach(3);
rServo.attach(11);
Neck.attach(8);
lServo.write(lZero);
rServo.write(rZero);
Neck.write(fHead);
}

void loop() {
// check everything every 10 seconds
current = millis();
if(current - old >= 10) {
old = current;
// if something is being executed
if(exe != -1) {
numTimes += 10; // keep track
// if the time is up
if(numTimes >= times[exe]) {
// nothing is being executed anymore
numTimes = 0;
exe = -1;
lServo.write(lZero);
rServo.write(rZero);
Neck.write(fHead);
}
}
// if nothings being executed
else {
// new instructions?
if(Serial.available() > 0) {
char input[1];
Serial.readBytes(input, 1);
exe = int(input[0]);
Serial.println(exe);
// start the instructions
switch(exe) {
case 0:
lServo.write(117);
rServo.write(0);
break;
case 1:
lServo.write(180);
rServo.write(180);
break;
case 2:
lServo.write(0);
rServo.write(0);
break;
case 3:
lServo.write(180);
rServo.write(180);
break;
case 4:
Neck.write(fHead);
break;
case 5:
Neck.write(rHead);
break;
case 6:
Neck.write(lHead);
break;
case 7:
Serial.println(ping());
break;
}
}
}
}
}

Any help would be much obliged.
-Gabriel

This code compiles and uploads, but does not function correctly.

What does it do?

By adding debug info, ive found that it doesn't recognize any input from user, and one time it thought that exe was always = 10, and the other time it thought it was 255.

why don't you just do

exe = Serial.read();

?

also what kind of data are you sending it?

byte exe = -1;

Now there something that you don't see every day. It shows a certain lack of appreciation of data types.

The cool smiley looks good in the code but it would be better if the code were in code tags rather than quote tags. As others have asked, what does the program do when you run it ?

gabriel42:
I would like my computer to control a robot i am working on. there are seven commands, once they are finished they return the number of their command. Additionally, one command returns a value from my robot's ping sensor. The old version of this code had delays, but i am working to re-write it without any. This code compiles and uploads, but does not function correctly.

void loop()  {
  current = millis();
  if(current - old >= 10)  {
    old = current;

I'm pretty sure that the 4th line there is the problem. It is instantly assigning old = current. It doesnt allow the the section of code to be performed and is constantly causing if(current - old >= 10) to be repeated.
You need to place it at after all the other lines of code such as:

if(exe != -1)  {
      numTimes += 10; // keep track
      // if the time is up
      if(numTimes >= times[exe])  {
        // nothing is being executed anymore
        numTimes = 0;
        exe = -1;
        lServo.write(lZero);
        rServo.write(rZero);
        Neck.write(fHead);
        old = current;
        }
      }

UKHeliBob:

byte exe = -1;

Now there something that you don't see every day. It shows a certain lack of appreciation of data types.

Whoops. I've done that before, and it's messed up my programs before. apparently i haven't learned.

duality:
why don't you just do

exe = Serial.read();

?
Serial.read() - Arduino Reference

I had problems with Serial before and this seemed to fix it. When i get this code to work I'll try to use exe = Serial.read().

duality:
also what kind of data are you sending it?

sorry if that wasn't clear from the comment at the top of my code. Im sending numbers 0-7.

UKHeliBob:
As others have asked, what does the program do when you run it ?

When the code is uploaded, the robot stops its motors from turning, resets head and all other things in setup(). it then refuses to respond to any user input via serial communication.

rep8:

gabriel42:
I would like my computer to control a robot i am working on. there are seven commands, once they are finished they return the number of their command. Additionally, one command returns a value from my robot's ping sensor. The old version of this code had delays, but i am working to re-write it without any. This code compiles and uploads, but does not function correctly.

void loop()  {

current = millis();
  if(current - old >= 10)  {
    old = current;




I'm pretty sure that the 4th line there is the problem. It is instantly assigning old = current. It doesnt allow the the section of code to be performed and is constantly causing if(current - old >= 10) to be repeated.
You need to place it at after all the other lines of code such as:


if(exe != -1)  {
      numTimes += 10; // keep track
      // if the time is up
      if(numTimes >= times[exe])  {
        // nothing is being executed anymore
        numTimes = 0;
        exe = -1;
        lServo.write(lZero);
        rServo.write(rZero);
        Neck.write(fHead);
        old = current;
        }
      }

I will try that as soon as my computer is on. Thanks for the idea.

Thanks for all the help

If you are sending the numbers 1-7 are you sending the ascii '1'-'7' or the actual value 1-7

tobyb121:
If you are sending the numbers 1-7 are you sending the ascii '1'-'7' or the actual value 1-7

I'm not sure. I will try to figure that out. that could be why using Serial.readBytes() fixed my problem. I am entering numbers via X-CTU.

rep8:

void loop()  {

current = millis();
  if(current - old >= 10)  {
    old = current;




I'm pretty sure that the 4th line there is the problem. It is instantly assigning old = current. It doesnt allow the the section of code to be performed and is constantly causing if(current - old >= 10) to be repeated.
You need to place it at after all the other lines of code such as:

Updating it at the start of the block doesn't cause the IF statement to be 'constantly repeated' - it just means that any use of that variable within the following code would see the new value instead of the old value. Since the following code doesn't use that variable at all, it makes no difference whatsoever where it is updated within the block, as long as it is updated somewhere.

tobyb121:
If you are sending the numbers 1-7 are you sending the ascii '1'-'7' or the actual value 1-7

well, shouldn't

exe = int(input[0]);

take care of that even if i was sending chars?

No, because '1' and 1 are two different values. Look at an ASCII table, e.g. http://www.asciitable.com/.

Yes:
No, because '1' and 1 are two different values. Look at an ASCII table, e.g. http://www.asciitable.com/.
Thank you. It works now.