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