how to enhance this code?

Hi there!

Ive been working on a arduino based robot it has wireless comunications servos and h bridge and 2 dc motors, the mecanical part is quite done and te code im posting below is able to test all the motion and comunication stuff, now my new problem is that as mi code is very basic it cant make more than one action at a time ex i cant move eyes and mouth at the same time or move forward and blink the way my code is made is so basic it has to wait for one condition to finish to pass to another

how can i skip this linear wayof working of the code?
working with functions can do this?

i dont need th hole solution but some hints to develop for some kind of multitasking

Thanks a lot!

#include <SoftwareSerial.h>;

#define rxPin 2
#define txPin 1

char prevChar = 0;
int i = 0;

int a = 0;
SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);

void setup(){

pinMode(rxPin, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);

rfSerial.begin(2400); // begin serial connection with RF Link unit
Serial.begin(2400); // begin serial communication over USB to the computer
digitalWrite(13, HIGH);
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
}

void loop(){

byte someChar = 0;
someChar = rfSerial.read(); // read whatever the RF Link has to offer

if(someChar==45){
//move bot forward with h bridge

digitalWrite(12, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
delay(200);
}

if(someChar==54){
//move bot backwards
digitalWrite(12, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
delay(200);
}

if(someChar==73){
//move bot to the right
digitalWrite(12, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
delay(200);
}

if(someChar==77){
//move bot to the left
digitalWrite(12, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
delay(200);
}

if(someChar==34){
// blink eyes
for(i=0;i<20;i++){

digitalWrite(6, HIGH);
delayMicroseconds(850);
digitalWrite(6, LOW);
delay(20);
}
delay(50);

for(i=0;i<20;i++){
digitalWrite(6, HIGH);
delayMicroseconds(300);
digitalWrite(6, LOW);
delay(20);
}
delay(50);
}

if(someChar==43){
//open close mouth
for(i=0;i<20;i++){

digitalWrite(5, HIGH);
delayMicroseconds(750);
digitalWrite(5, LOW);
delay(20);
}
delay(50);

for(i=0;i<20;i++){
digitalWrite(5, HIGH);
delayMicroseconds(550);
digitalWrite(5, LOW);
delay(20);
}
delay(50);
}

// digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
Serial.println(someChar,DEC);

}

The first thing I would recommend is putting your "moves" into functions, like this:

void moveForward() { digitalWrite(12, HIGH); digitalWrite(9, HIGH); digitalWrite(11, LOW); digitalWrite(10, HIGH); digitalWrite(8, HIGH); digitalWrite(7, LOW); }

Now you can construct logic to control your bot, using conditional statements in the loop() function:

void loop() { if (bumper == 0) { moveForward(); delay(200); } else { moveBackward(); delay(200); } }

A more effective way to remove time-dependent outputs from your main loop might be to relocate your control logic to a "state machine" function:

void loop() { sensorPoll(); // a function to poll sensors and smooth data if required outputChange(); // this is where your State Machine lives, based on sensor data it determines whether you need to update // motor outputs if (changeFlag == TRUE) { outputAdjust(); } }

...now your PWM output levels are being adjusted only when needed (by outputAdjust(), based on input from outputChange() )

You have to think about what the robot does a bit differently to do this; instead of asking "What does the robot do next?" think "What state changes next?"

Hi there!

Hi. :)

While you have a very cool looking robot there--I particularly liked the eyes--it would be appreciated if you could read my recent post on why "cross-posting" is bad idea.

I think eustace makes a good suggestion in relation to using something like a state machine to enhance your code. Note that it will mean that if your robot depends on particular time delays for certain functions--like bllinking those eyes :D-- you'll need to track that yourself, probably making use of millis().

--Phil.