Adding a Subroutine to a perfectly good program

Adding a Subroutine to a perfectly good program that is already working is what I wish to do. I accidently posted in the wrong area.

here is my code:

// Begin Robot Code
int micVal;
int cdsVal;
int irLval; // Left IR
int irCval; // Center IR
int irRval; // Right IR
int irRRval; // Rear IR
int i; // Generic Counter
int x; // Generic Counter
int PLval; // Pulse Width for Left Servo
int PRval; // Pulse Width for Right Servo
int cntr; // Generic Counter Used for Determining amt. of Object Detections
int counter; // Generic Counter
int clrpth; // amt. of Milliseconds Of Unobstructed Path
int objdet; // Time an Object was Detected
int task; // Routine to Follow for Clearest Path
int pwm; // Pulse Width for Pan Servo
boolean add; // Whether to Increment or Decrement PW Value for Pan Servo
int distance; // Distance to Object Detected via Ultrasonic Ranger
int oldDistance; // Previous Distance Value Read from Ultrasonic Ranger

float scale = 1.9866666666666666666666666666667; // Not Currently Used

int LeftPin = 6; // Left Servo
int RightPin = 9; // Right Servo
int PiezoPin = 11; // Piezo
int PingServoPin = 5; // Pan Servo
int irLPin = 0; // Analog 0; Left IR
int irCPin = 1; // Analog 1; Center IR
int irRPin = 2; // Analog 2; Right IR
int irRRPin =3; // Analog 3; Rear IR
int BrowservoPin = 4; // Analog 4; Brow Servo
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0; // Used for Ultrasonic Ranger
int ultrasoundValue = 0; // Raw Distance Val
int oldUltrasoundValue; // Not used
int pulseCount; // Generic Counter
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13

#define BAUD 9600
#define CmConstant 1/29.034

void setup() {
Serial.begin(9600);
pinMode(PiezoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(LeftPin, OUTPUT);
pinMode(PingServoPin, OUTPUT);
pinMode(BrowservoPin, OUTPUT);
pinMode(irLPin, INPUT);
pinMode(irCPin, INPUT);
pinMode(irRPin, INPUT);
pinMode(irRRPin, INPUT);

for(i = 0; i < 500; i++) {
digitalWrite(PiezoPin, HIGH);
delayMicroseconds(1000);
digitalWrite(PiezoPin, LOW);
delayMicroseconds(1000);
}
for(i = 0; i < 20; i++) {
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(655 * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
}
ultrasoundValue = 600;
i = 0;
}
//InitializeBrows();
do while (i != 40); i++;
PULSOUT BrowServoPin, 400
delay (500)
{
//Scan();
Look();
Go();
}
void Look() {
irLval = analogRead(irLPin);
irCval = analogRead(irCPin);
irRval = analogRead(irRPin);
irRRval = analogRead(irRRPin);
//if(counter > 10) {
//counter = 0;
//readPing();
//}
if(irLval > 200) {
PLval = 850;
PRval = 820;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irCval > 200) {
PLval = 850;
PRval = 820;
x = 10;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irRval > 200) {
PLval = 650;
PRval = 620;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else {
x = 1;
PLval = 850;
PRval = 620;
counter = counter + 1;
clrpth = (millis() - objdet);
if(add == true) {
pwm = pwm + 50;
}
else if(add == false) {
pwm = pwm - 50;
}
if(pwm < 400) {
pwm = 400;
add = true;
}
if(pwm > 950) {
pwm = 950;
add = false;
}
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
readPing();
if(ultrasoundValue < 500) {
cntr = cntr + 1;
switch(pwm) {
case 400:
x = 7;
PLval = 650;
PRval = 650;
Go();
break;
case 500:
x = 10;
PLval = 650;
PRval = 650;
Go();
break;
case 600:
x = 14;
PLval = 850;
PRval = 850;
Go();
break;
case 700:
x = 10;
PLval = 850;
PRval = 850;
Go();
break;
case 950:
x = 7;
PLval = 850;
PRval = 850;
Go();
break;
}
}
}
//Serial.print("clrpth: ");
//Serial.println(clrpth);
//Serial.print("objdet: ");
//Serial.println(objdet);
//Serial.print("cntr: ");
//Serial.println(cntr);
if(cntr > 25 && clrpth < 2000) {
clrpth = 0;
cntr = 0;
Scan();
}
}
void Go() {
for(i = 0; i < x; i++) {
digitalWrite(LeftPin, HIGH);
delayMicroseconds(PLval * 2);
digitalWrite(LeftPin, LOW);
digitalWrite(RightPin, HIGH);
delayMicroseconds(PRval * 2);
digitalWrite(RightPin, LOW);
delay(20);
}
}
void readPing() { // Get Distance from Ultrasonic Ranger
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port


*/

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

//serialWrite(‘A’); // Example identifier for the sensor
//printInteger(ultrasoundValue);
//serialWrite(10);
//serialWrite(13);

/* Lite up LED if any value is passed by the echo pulse


*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
}

void Scan() { // Scan for the Clearest Path
oldDistance = 30;
task = 0;
for(i = 1; i < 5; i++) {
switch(i) {
case 1:
//Serial.println(“Pos. 1”);
pwm = 1125; /// incr. by 100 from 1085
break;
case 2:
//Serial.println(“Pos. 2”);
pwm = 850; //// increased by 100 from 850
break;
case 3:
//Serial.println(“Pos. 3”);
pwm = 400;
break;
case 4:
//Serial.println(“Pos. 4”);
pwm = 235;
break;
}
for(pulseCount = 0; pulseCount < 20; pulseCount++) { // Adjust Pan Servo and Read USR
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
readPing();
delay(20);
}
distance = ((float)ultrasoundValue * CmConstant); // Calculate Distance in Cm
if(distance > oldDistance) { // If the Newest distance is longer, replace previous reading with it
oldDistance = distance;
task = i; // Set task equal to Pan Servo Position
}
}
//Serial.print("Task: ");
//Serial.println(task);
//Serial.print("distance: ");
//Serial.println(distance);
//Serial.print("oldDistance: ");
//Serial.println(oldDistance);
distance = 50; // Prevents Scan from Looping
switch(task) { // Determine which task should be carried out
case 0: // Center was clearest
x = 28;
PLval = (850);
PRval = (850);
Go();
break;
case 1: // 90 degrees Left was Clearest
x = 14;
PLval = (650);
PRval = (650);
Go();
break;
case 2: // 45 degrees left
x = 7;
PLval = (650);
PRval = (650);
Go();
break;
case 3: // 45 degrees right
x = 7;
PLval = (850);
PRval = (850);
Go();
break;
case 4: // 90 degrees right
x = 14;
PLval = (850);
PRval = (850);
Go();
break;

// Brow Control
/void InitializeBrows()
switch(task) { // Determine which task should be carried out
case 0: // Initialize Brows
do while (i != 40); i++;
PULSOUT BrowservoPin, 400
delay (500)
InitializeBrows();
break;
case 1: // Raise Brows
void RaiseBrows() {
do while (i != 40); i++;
PULSOUT BrowservoPin, 15
delay (500)
RaiseBrows();
break;
case 2: // Lower Brows
void LowerBrows(); {
do while (i != 40); i++;
PULSOUT BrowservoPin, 600
delay (500)
LowerBrows();
break;

}
}

// End Robot Code

I get the:expected unqualified-id before numeric constant In function ‘void Scan()’ ERROR ???

I got this error when trying to add BrowServo for my Eyebrows. I want them to go up when the IR or Ping sees something. It works great on my OTHER robot in PBASIC.

I will try to keep this in the right place from now on.

Admin, please feel free to erase the other posts.

Looks like you're missing some braces in your Setup(). The line starting"//InitializeBrows();" is not in the body of a routine.

You can avoid such mistakes by being careful with your indenting and following a standard code formatting practice.

Edit: Ah, or it could be that you need to turn that line into a proper function header?

void InitializeBrows(){

?

Thanks, Dave, I will try those. I am not a c programmer, I am sure I will get caught up some day.

I kinda rearranged things and I get:

expected constructor, destructor, or type conversion before ‘;’

with this code. I am trying to do two things at once. I am adding EyeBrows to go up and down when the robot sees something in front of it. Also,I am adding another SharpIR sensor to the Rear so it can check if it can backup.
Here is my latest code:

// Begin Robot Code
int micVal;
int cdsVal;
int irLval; // Left IR
int irCval; // Center IR
int irRval; // Right IR
int irRRval; // Rear IR
int i; // Generic Counter
int x; // Generic Counter
int PLval; // Pulse Width for Left Servo
int PRval; // Pulse Width for Right Servo
int cntr; // Generic Counter Used for Determining amt. of Object Detections
int counter; // Generic Counter
int clrpth; // amt. of Milliseconds Of Unobstructed Path
int objdet; // Time an Object was Detected
int task; // Routine to Follow for Clearest Path
int pwm; // Pulse Width for Pan Servo
boolean add; // Whether to Increment or Decrement PW Value for Pan Servo
int distance; // Distance to Object Detected via Ultrasonic Ranger
int oldDistance; // Previous Distance Value Read from Ultrasonic Ranger

float scale = 1.9866666666666666666666666666667; // Not Currently Used

int LeftPin = 6; // Left Servo
int RightPin = 9; // Right Servo
int PiezoPin = 11; // Piezo
int PingServoPin = 5; // Pan Servo
int irLPin = 0; // Analog 0; Left IR
int irCPin = 1; // Analog 1; Center IR
int irRPin = 2; // Analog 2; Right IR
int irRRPin =3; // Analog 3; Rear IR
int BrowservoPin = 4; // Analog 4; Brow Servo
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0; // Used for Ultrasonic Ranger
int ultrasoundValue = 0; // Raw Distance Val
int oldUltrasoundValue; // Not used
int pulseCount; // Generic Counter
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13

#define BAUD 9600
#define CmConstant 1/29.034

void setup() {
Serial.begin(9600);
pinMode(PiezoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(LeftPin, OUTPUT);
pinMode(PingServoPin, OUTPUT);
pinMode(BrowservoPin, OUTPUT);
pinMode(irLPin, INPUT);
pinMode(irCPin, INPUT);
pinMode(irRPin, INPUT);
pinMode(irRRPin, INPUT);

for(i = 0; i < 500; i++) {
digitalWrite(PiezoPin, HIGH);
delayMicroseconds(1000);
digitalWrite(PiezoPin, LOW);
delayMicroseconds(1000);
}
for(i = 0; i < 20; i++) {
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(655 * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
}
ultrasoundValue = 600;
i = 0;
}

//Scan();
Look();
Go();
InitializeBrows();
}
void Look() {
irLval = analogRead(irLPin);
irCval = analogRead(irCPin);
irRval = analogRead(irRPin);
irRRval = analogRead(irRRPin);
//if(counter > 10) {
//counter = 0;
//readPing();
//}
if(irLval > 200) {
PLval = 850;
PRval = 820;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irCval > 200) {
PLval = 850;
PRval = 820;
x = 10;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irRval > 200) {
PLval = 650;
PRval = 620;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else {
x = 1;
PLval = 850;
PRval = 620;
counter = counter + 1;
clrpth = (millis() - objdet);
if(add == true) {
pwm = pwm + 50;
}
else if(add == false) {
pwm = pwm - 50;
}
if(pwm < 400) {
pwm = 400;
add = true;
}
if(pwm > 950) {
pwm = 950;
add = false;
}
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
readPing();
if(ultrasoundValue < 500) {
cntr = cntr + 1;
switch(pwm) {
case 400:
x = 7;
PLval = 650;
PRval = 650;
Go();
break;
case 500:
x = 10;
PLval = 650;
PRval = 650;
Go();
break;
case 600:
x = 14;
PLval = 850;
PRval = 850;
Go();
break;
case 700:
x = 10;
PLval = 850;
PRval = 850;
Go();
break;
case 950:
x = 7;
PLval = 850;
PRval = 850;
Go();
break;
}
}
}
//Serial.print("clrpth: ");
//Serial.println(clrpth);
//Serial.print("objdet: ");
//Serial.println(objdet);
//Serial.print("cntr: ");
//Serial.println(cntr);
if(cntr > 25 && clrpth < 2000) {
clrpth = 0;
cntr = 0;
Scan();
}
}
void Go() {
for(i = 0; i < x; i++) {
digitalWrite(LeftPin, HIGH);
delayMicroseconds(PLval * 2);
digitalWrite(LeftPin, LOW);
digitalWrite(RightPin, HIGH);
delayMicroseconds(PRval * 2);
digitalWrite(RightPin, LOW);
delay(20);
}
}
void readPing() { // Get Distance from Ultrasonic Ranger
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port


*/

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

//serialWrite(‘A’); // Example identifier for the sensor
//printInteger(ultrasoundValue);
//serialWrite(10);
//serialWrite(13);

/* Lite up LED if any value is passed by the echo pulse


*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
}

void InitializeBrows(); {
do while (i != 40); i++;
PULSOUT BrowServoPin, 400
delay (500)

void Scan() { // Scan for the Clearest Path
oldDistance = 30;
task = 0;
for(i = 1; i < 5; i++) {
switch(i) {
case 1:
//Serial.println(“Pos. 1”);
pwm = 1125; /// incr. by 100 from 1085
break;
case 2:
//Serial.println(“Pos. 2”);
pwm = 850; //// increased by 100 from 850
break;
case 3:
//Serial.println(“Pos. 3”);
pwm = 400;
break;
case 4:
//Serial.println(“Pos. 4”);
pwm = 235;
break;
}
for(pulseCount = 0; pulseCount < 20; pulseCount++) { // Adjust Pan Servo and Read USR
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
readPing();
delay(20);
}
distance = ((float)ultrasoundValue * CmConstant); // Calculate Distance in Cm
if(distance > oldDistance) { // If the Newest distance is longer, replace previous reading with it
oldDistance = distance;
task = i; // Set task equal to Pan Servo Position
}
}
//Serial.print("Task: ");
//Serial.println(task);
//Serial.print("distance: ");
//Serial.println(distance);
//Serial.print("oldDistance: ");
//Serial.println(oldDistance);
distance = 50; // Prevents Scan from Looping
switch(task) { // Determine which task should be carried out
case 0: // Center was clearest
x = 28;
PLval = (850);
PRval = (850);
Go();
break;
case 1: // 90 degrees Left was Clearest
x = 14;
PLval = (650);
PRval = (650);
Go();
break;
case 2: // 45 degrees left
x = 7;
PLval = (650);
PRval = (650);
Go();
break;
case 3: // 45 degrees right
x = 7;
PLval = (850);
PRval = (850);
Go();
break;
case 4: // 90 degrees right
x = 14;
PLval = (850);
PRval = (850);
Go();
break;

// Brow Control
void InitializeBrows();
switch(task) { // Determine which task should be carried out
case 0: // Initialize Brows
do while (i != 40); i++;
PULSOUT BrowservoPin, 400
delay (500)
InitializeBrows();
break;
case 1: // Raise Brows
void RaiseBrows() {
do while (i != 40); i++;
PULSOUT BrowservoPin, 15
delay (500)
RaiseBrows();
break;
case 2: // Lower Brows
void LowerBrows(); {
do while (i != 40); i++;
PULSOUT BrowservoPin, 600
delay (500)
LowerBrows();
break;

Looks like we lost some of the tail end of that one, maybe too big for one post?

Also, most forum members prefer code to be posted in [ code ] tags, use the '#' button in the tool bar.

try this:

// Begin Robot Code
int micVal;
int cdsVal;
int irLval; // Left IR
int irCval; // Center IR
int irRval; // Right IR
int irRRval; // Rear IR
int i; // Generic Counter
int x; // Generic Counter
int PLval; // Pulse Width for Left Servo
int PRval; // Pulse Width for Right Servo
int cntr; // Generic Counter Used for Determining amt. of Object Detections
int counter; // Generic Counter
int clrpth; // amt. of Milliseconds Of Unobstructed Path
int objdet; // Time an Object was Detected
int task; // Routine to Follow for Clearest Path
int pwm; // Pulse Width for Pan Servo
boolean add; // Whether to Increment or Decrement PW Value for Pan Servo
int distance; // Distance to Object Detected via Ultrasonic Ranger
int oldDistance; // Previous Distance Value Read from Ultrasonic Ranger

float scale = 1.9866666666666666666666666666667; // Not Currently Used

int LeftPin = 6; // Left Servo
int RightPin = 9; // Right Servo
int PiezoPin = 11; // Piezo
int PingServoPin = 5; // Pan Servo
int irLPin = 0; // Analog 0; Left IR
int irCPin = 1; // Analog 1; Center IR
int irRPin = 2; // Analog 2; Right IR
int irRRPin =3; // Analog 3; Rear IR
int BrowservoPin = 4; // Analog 4; Brow Servo
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0; // Used for Ultrasonic Ranger
int ultrasoundValue = 0; // Raw Distance Val
int oldUltrasoundValue; // Not used
int pulseCount; // Generic Counter
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13

#define BAUD 9600
#define CmConstant 1/29.034

void setup() {
Serial.begin(9600);
pinMode(PiezoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(LeftPin, OUTPUT);
pinMode(PingServoPin, OUTPUT);
pinMode(BrowservoPin, OUTPUT);
pinMode(irLPin, INPUT);
pinMode(irCPin, INPUT);
pinMode(irRPin, INPUT);
pinMode(irRRPin, INPUT);

for(i = 0; i < 500; i++) {
digitalWrite(PiezoPin, HIGH);
delayMicroseconds(1000);
digitalWrite(PiezoPin, LOW);
delayMicroseconds(1000);
}
for(i = 0; i < 20; i++) {
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(655 * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
}
ultrasoundValue = 600;
i = 0;
}

//Scan();
Look();
Go();
InitializeBrows();
}
void Look() {
irLval = analogRead(irLPin);
irCval = analogRead(irCPin);
irRval = analogRead(irRPin);
irRRval = analogRead(irRRPin);
//if(counter > 10) {
//counter = 0;
//readPing();
//}
if(irLval > 200) {
PLval = 850;
PRval = 820;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irCval > 200) {
PLval = 850;
PRval = 820;
x = 10;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irRval > 200) {
PLval = 650;
PRval = 620;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else {
x = 1;
PLval = 850;
PRval = 620;
counter = counter + 1;
clrpth = (millis() - objdet);
if(add == true) {
pwm = pwm + 50;
}
else if(add == false) {
pwm = pwm - 50;
}
if(pwm < 400) {
pwm = 400;
add = true;
}
if(pwm > 950) {
pwm = 950;
add = false;
}
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
readPing();
if(ultrasoundValue < 500) {
cntr = cntr + 1;
switch(pwm) {
case 400:
x = 7;
PLval = 650;
PRval = 650;
Go();
break;
case 500:
x = 10;
PLval = 650;
PRval = 650;
Go();
break;
case 600:
x = 14;
PLval = 850;
PRval = 850;
Go();
break;
case 700:
x = 10;
PLval = 850;
PRval = 850;
Go();
break;
case 950:
x = 7;
PLval = 850;
PRval = 850;
Go();
break;
}
}
}
//Serial.print("clrpth: ");
//Serial.println(clrpth);
//Serial.print("objdet: ");
//Serial.println(objdet);
//Serial.print("cntr: ");
//Serial.println(cntr);
if(cntr > 25 && clrpth < 2000) {
clrpth = 0;
cntr = 0;
Scan();
}
}
void Go() {
for(i = 0; i < x; i++) {
digitalWrite(LeftPin, HIGH);
delayMicroseconds(PLval * 2);
digitalWrite(LeftPin, LOW);
digitalWrite(RightPin, HIGH);
delayMicroseconds(PRval * 2);
digitalWrite(RightPin, LOW);
delay(20);
}
}
void readPing() { // Get Distance from Ultrasonic Ranger
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port


*/

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

//serialWrite(‘A’); // Example identifier for the sensor
//printInteger(ultrasoundValue);
//serialWrite(10);
//serialWrite(13);

/* Lite up LED if any value is passed by the echo pulse


*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
}

void InitializeBrows(); {
do while (i != 40); i++;
PULSOUT BrowServoPin, 400
delay (500)

void Scan() { // Scan for the Clearest Path
oldDistance = 30;
task = 0;
for(i = 1; i < 5; i++) {
switch(i) {
case 1:
//Serial.println(“Pos. 1”);
pwm = 1125; /// incr. by 100 from 1085
break;
case 2:
//Serial.println(“Pos. 2”);
pwm = 850; //// increased by 100 from 850
break;
case 3:
//Serial.println(“Pos. 3”);
pwm = 400;
break;
case 4:
//Serial.println(“Pos. 4”);
pwm = 235;
break;
}
for(pulseCount = 0; pulseCount < 20; pulseCount++) { // Adjust Pan Servo and Read USR
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
readPing();
delay(20);
}
distance = ((float)ultrasoundValue * CmConstant); // Calculate Distance in Cm
if(distance > oldDistance) { // If the Newest distance is longer, replace previous reading with it
oldDistance = distance;
task = i; // Set task equal to Pan Servo Position
}
}
//Serial.print("Task: ");
//Serial.println(task);
//Serial.print("distance: ");
//Serial.println(distance);
//Serial.print("oldDistance: ");
//Serial.println(oldDistance);
distance = 50; // Prevents Scan from Looping
switch(task) { // Determine which task should be carried out
case 0: // Center was clearest
x = 28;
PLval = (850);
PRval = (850);
Go();
break;
case 1: // 90 degrees Left was Clearest
x = 14;
PLval = (650);
PRval = (650);
Go();
break;
case 2: // 45 degrees left
x = 7;
PLval = (650);
PRval = (650);
Go();
break;
case 3: // 45 degrees right
x = 7;
PLval = (850);
PRval = (850);
Go();
break;
case 4: // 90 degrees right
x = 14;
PLval = (850);
PRval = (850);
Go();
break;

// Brow Control
void InitializeBrows();
switch(task) { // Determine which task should be carried out
case 0: // Initialize Brows
do while (i != 40); i++;
PULSOUT BrowservoPin, 400
delay (500)
InitializeBrows();
break;
case 1: // Raise Brows
void RaiseBrows() {
do while (i != 40); i++;
PULSOUT BrowservoPin, 15
delay (500)
RaiseBrows();
break;
case 2: // Lower Brows
void LowerBrows(); {
do while (i != 40); i++;
PULSOUT BrowservoPin, 600
delay (500)
LowerBrows();
break;

}
}

// End Robot Code

Ok. You have some basic syntax errors that are causing most of your problems.

For example, check the setup function. I’m going to remove most of the code and just leave the opening and closing braces:

void setup() {
    for(i = 0; i < 500; i++) {
    }
    for(i = 0; i < 20; i++) {
    }
    ultrasoundValue = 600;
    i = 0;
}

//Scan();
Look();
Go();
InitializeBrows();
}

Notice how those last 5 lines, including the closing brace, are not part of function.

Then there is some code that looks like this:

void InitializeBrows(); {
    do while (i != 40);
    i++;
    PULSOUT BrowServoPin, 400
    delay (500)

You’ll need to fix the function header, no semicolon before the opening brace. The while loop is empty because it has a semicolon at the end before the code. And it looks like you’ve got some more translation to do there too. There are a couple of other places with similar issues.

I’d recommend starting at the top and going through fixing up your indenting so that you can see the structure better. Every { increases the indention level by one, every } decreases it.

Thanks, Dave. I will try all of this. I have a lot to learn. But getting there is half of the fun. Hopefully , I will make new mistakes instead of the same ones.

God Bless! :smiley:

You reply really cleared a lot of stuff up. It made me understand the programming language more. I am down to where I and geting some errors , but they are way down there. I feel good about that. Thanks.

No problem! Glad you're having fun with it :slight_smile:

I am doing fine. I am down where the PulseOUT command is. I do not see it in the Arduino Reference manual. But, I probably just have to look harder.

:slight_smile:

I don't believe there is currently a PulseOut function, you'll have to use a couple of digitalWrite()s and a delay(). Unless you need some very short pulses (a few microseconds) you shouldn't have a problem with digitalWrite.

I think there are plans to incorporate a pulseOut function in an upcoming version, there have been a number of requests for it.

OK, so it is digitalwrite and delay. What if it is on an analog pin ? do I need to change it to a digital one?

Thanks!

Update: here is my latest code:
a function-definition is not allowed here before ‘{’ token ERROR.
// Begin Robot Code
int micVal;
int cdsVal;
int irLval; // Left IR
int irCval; // Center IR
int irRval; // Right IR
int irRRval; // Rear IR
int i; // Generic Counter
int x; // Generic Counter
int PLval; // Pulse Width for Left Servo
int PRval; // Pulse Width for Right Servo
int cntr; // Generic Counter Used for Determining amt. of Object Detections
int counter; // Generic Counter
int clrpth; // amt. of Milliseconds Of Unobstructed Path
int objdet; // Time an Object was Detected
int task; // Routine to Follow for Clearest Path
int pwm; // Pulse Width for Pan Servo
boolean add; // Whether to Increment or Decrement PW Value for Pan Servo
int distance; // Distance to Object Detected via Ultrasonic Ranger
int oldDistance; // Previous Distance Value Read from Ultrasonic Ranger

float scale = 1.9866666666666666666666666666667; // Not Currently Used

int LeftPin = 6; // Left Servo
int RightPin = 9; // Right Servo
int PiezoPin = 11; // Piezo
int PingServoPin = 5; // Pan Servo
int irLPin = 0; // Analog 0; Left IR
int irCPin = 1; // Analog 1; Center IR
int irRPin = 2; // Analog 2; Right IR
int irRRPin =3; // Analog 3; Rear IR
int BrowservoPin = 4; // Analog 4; Brow Servo
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0; // Used for Ultrasonic Ranger
int ultrasoundValue = 0; // Raw Distance Val
int oldUltrasoundValue; // Not used
int pulseCount; // Generic Counter
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13

#define BAUD 9600
#define CmConstant 1/29.034

void setup() {
Serial.begin(9600);
pinMode(PiezoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(LeftPin, OUTPUT);
pinMode(PingServoPin, OUTPUT);
pinMode(BrowservoPin, OUTPUT);
pinMode(irLPin, INPUT);
pinMode(irCPin, INPUT);
pinMode(irRPin, INPUT);
pinMode(irRRPin, INPUT);

for(i = 0; i < 500; i++) {
digitalWrite(PiezoPin, HIGH);
delayMicroseconds(1000);
digitalWrite(PiezoPin, LOW);
delayMicroseconds(1000);
}
for(i = 0; i < 20; i++) {
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(655 * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
}
ultrasoundValue = 600;
i = 0;

Look();
Go();
readPing();
Scan();
InitializeBrows();

}
void Look() {
irLval = analogRead(irLPin);
irCval = analogRead(irCPin);
irRval = analogRead(irRPin);
irRRval = analogRead(irRRPin);
//if(counter > 10) {
//counter = 0;
//readPing();
//}
if(irLval > 200) {
PLval = 850;
PRval = 820;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irCval > 200) {
PLval = 850;
PRval = 820;
x = 10;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else if(irRval > 200) {
PLval = 650;
PRval = 620;
x = 5;
cntr = cntr + 1;
clrpth = 0;
objdet = millis();
}
else {
x = 1;
PLval = 850;
PRval = 620;
counter = counter + 1;
clrpth = (millis() - objdet);
if(add == true) {
pwm = pwm + 50;
}
else if(add == false) {
pwm = pwm - 50;
}
if(pwm < 400) {
pwm = 400;
add = true;
}
if(pwm > 950) {
pwm = 950;
add = false;
}
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
delay(20);
readPing();
if(ultrasoundValue < 500) {
cntr = cntr + 1;
switch(pwm) {
case 400:
x = 7;
PLval = 650;
PRval = 650;
Go();
break;
case 500:
x = 10;
PLval = 650;
PRval = 650;
Go();
break;
case 600:
x = 14;
PLval = 850;
PRval = 850;
Go();
break;
case 700:
x = 10;
PLval = 850;
PRval = 850;
Go();
break;
case 950:
x = 7;
PLval = 850;
PRval = 850;
Go();
break;
}
}
}
//Serial.print("clrpth: ");
//Serial.println(clrpth);
//Serial.print("objdet: ");
//Serial.println(objdet);
//Serial.print("cntr: ");
//Serial.println(cntr);
if(cntr > 25 && clrpth < 2000) {
clrpth = 0;
cntr = 0;
Scan();
}
}
void Go() {
for(i = 0; i < x; i++) {
digitalWrite(LeftPin, HIGH);
delayMicroseconds(PLval * 2);
digitalWrite(LeftPin, LOW);
digitalWrite(RightPin, HIGH);
delayMicroseconds(PRval * 2);
digitalWrite(RightPin, LOW);
delay(20);
}
}
void readPing() { // Get Distance from Ultrasonic Ranger
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port


*/

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

//serialWrite(‘A’); // Example identifier for the sensor
//printInteger(ultrasoundValue);
//serialWrite(10);
//serialWrite(13);

/* Lite up LED if any value is passed by the echo pulse


*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
}

void Scan() { // Scan for the Clearest Path
oldDistance = 30;
task = 0;
for(i = 1; i < 5; i++) {
switch(i) {
case 1:
//Serial.println(“Pos. 1”);
pwm = 1125; /// incr. by 100 from 1085
break;
case 2:
//Serial.println(“Pos. 2”);
pwm = 850; //// increased by 100 from 850
break;
case 3:
//Serial.println(“Pos. 3”);
pwm = 400;
break;
case 4:
//Serial.println(“Pos. 4”);
pwm = 235;
break;
}
for(pulseCount = 0; pulseCount < 20; pulseCount++) { // Adjust Pan Servo and Read USR
digitalWrite(PingServoPin, HIGH);
delayMicroseconds(pwm * 2);
digitalWrite(PingServoPin, LOW);
readPing();
delay(20);
}
distance = ((float)ultrasoundValue * CmConstant); // Calculate Distance in Cm
if(distance > oldDistance) { // If the Newest distance is longer, replace previous reading with it
oldDistance = distance;
task = i; // Set task equal to Pan Servo Position
}
}
//Serial.print("Task: ");
//Serial.println(task);
//Serial.print("distance: ");
//Serial.println(distance);
//Serial.print("oldDistance: ");
//Serial.println(oldDistance);
distance = 50; // Prevents Scan from Looping
switch(task) { // Determine which task should be carried out
case 0: // Center was clearest
x = 28;
PLval = (850);
PRval = (850);
Go();
break;
case 1: // 90 degrees Left was Clearest
x = 14;
PLval = (650);
PRval = (650);
Go();
break;
case 2: // 45 degrees left
x = 7;
PLval = (650);
PRval = (650);
Go();
break;
case 3: // 45 degrees right
x = 7;
PLval = (850);
PRval = (850);
Go();
break;
case 4: // 90 degrees right
x = 14;
PLval = (850);
PRval = (850);
Go();
break;
}

}
// Brow Control
void InitializeBrows() {
distance = 50;
switch(task) { // Determine which task should be carried out
case 0: // Initialize Brows
for ((i != 40); i++ :wink:
digitalWrite(BrowservoPin,HIGH);
delayMicroseconds(400);
digitalWrite(BrowservoPin,LOW);
InitializeBrows();
break;
case 1: // Raise Brows
void RaiseBrows(); {
for ((i != 40); i++ ; )
digitalWrite(BrowservoPin,HIGH);
delayMicroseconds(500);
digitalWrite(BrowservoPin,LOW);
RaiseBrows();
break;
case 2: // Lower Brows
void LowerBrows(); {
for ((i != 40); i++ :wink:
digitalWrite(BrowservoPin,HIGH);
delayMicroseconds(600);
digitalWrite(BrowservoPin,LOW);
LowerBrows();
break;
}
}

// End Robot Code

Looks like you have some issues in InitializeBrows().

Your for-loop needs to be finished here:

            case 0:  // Initialize Brows
                  for  ((i != 40); i++ Wink  
                  digitalWrite(BrowservoPin,HIGH);

Then in case 1 and 2 you have the same problem with the for-loop, plus you have what looks like a function declaration within the code block:

            case 1:  // Raise Brows
                  void RaiseBrows(); {
                        for  ((i != 40);  i++ ; )

Thanks, How do I finish it? I hate sound so ignorant. I will look it up.

Just a little Update.

I finally got all of my brackets straight and in good order. I fixed the for loop and everything looked good. I went to compile and I got:

o: In function InitializeBrows()': C:\DOCUME~1\MOVIEM~1\LOCALS~1\Temp\build57706.tmp/Temporary_7858_3532.cpp:348: undefined reference to RaiseBrows()'

C:\DOCUME~1\MOVIEM~1\LOCALS~1\Temp\build57706.tmp/Temporary_7858_3532.cpp:358: undefined reference to `LowerBrows()'

o: In function main': C:\DOCUME~1\MOVIEM~1\LOCALS~1\Temp\build57706.tmp/Temporary_7858_3532.cpp:377: undefined reference to loop'

Couldn't determine program size: C:\arduino-0016\hardware/tools/avr/bin/avr-size: 'C:\DOCUME~1\MOVIEM~1\LOCALS~1\Temp\build57706.tmp\Lars.hex': No such file

I have no idea what this means, but I am going to look it up in the reference section.

My Question still remains. I have two programs that work perfectly by themselves. I just need to intergrate one into the other. I want to use one as a subroutine to call many times and go back where it came from. I believe the break; word will be the RETURN command. Now, I just need to know how to do the rest of it. I have two void setup() s, one in each program. I don’t think I can put two in one program can I?

Forgive me for my Ignorance.

You guys have been very helpful so far. Thanks! I appreciate it.

:wink:

One setup function only, you will have to combine any initialization code from the two programs into the single setup function.

As far as how to call the second program from the first, you will have to 'wrap' the second program inside a new function statement which will include the code from your second program. There is no need for a break from the new function as it will proceed to the next statement on the calling program when it completes. The return word is used to return/assign a value to calling program if it is required in your coding, otherwise the return is automatic when the new function reaches it's last statement.

Lefty

The return word is used to return/assign a value to calling program if it is required in your coding, otherwise the return is automatic when the new function reaches it's last statement.

You can also use a "return" without a value in a "void" subroutine, to return from the subroutine before the final brace.

e.g.

void mySub (void)
{
..
..
  if (someCondition) {
    return;  // return from subroutine "early"
  }
..
// program execution would normally reach here and automatically return
}