I have been slowly adding things to my little robot borrowing code from here and there and slowly learning to write my own. I have and Uno Rev 3 with and Adafruit motorshield and i have added a parallax Ping and recently a 16x2 LCd readout.
My problem is this. I have code that works and does exactly what i want it o do. basic collision avoidance with serial and LCD status display. The trouble is everytime I add a new piece I have to rearrange and make the code work all over. So i decided to get inline and start writing each piece as a function and Thought I had it figured out. The trouble is it seems to slow the code so much that it doesn't perform well anymore. I will post both sketches and any tips will be greatly appreciated.
This sketch works fine and no apparent delays.
//Woody Bot
#include <Ping.h>
#include <AFMotor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
Ping ping = Ping(A3); //set which model ping your using
AF_DCMotor motor(1, MOTOR12_64KHZ); //Set motors to right frequency to match your servo.
// create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ);
// create motor #2, 64KHz pwm
int count = 0;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // start serial communication at 9600 baud
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.print(" Woody Bot");// Print a message to the LCD.
lcd.setCursor(0,1);
lcd.print(" Version 1.4"); //Print on line 2 of the lcd
delay(3000);
pinMode(A0, INPUT_PULLUP); // make pin A0 an input with internal pullup
motor.setSpeed(255); // set the speed 0f the servos
motor2.setSpeed(130);
while (digitalRead(A0) == HIGH){ // Pause until the button is pressed
lcd.setCursor(0,1);
lcd.print(" Press Start! ");
}
}
void loop()
{
if(count == 0) {
for(int i = 0; i<5; i++) { // do this 5 times
ping.fire();
int inches = ping.inches(); // where is everything?
int cm = ping.centimeters();
int mseconds = ping.microseconds();
Serial.print(inches); // print values out on serial monitor
Serial.print(" , ");
Serial.print(cm);
Serial.print(" , ");
Serial.println(mseconds);
delay(100);
if(inches <= 6) { // if the Woody Bot gets too close...
lcd.setCursor(0,1);
lcd.print(" Turn Left ");
motor.run(BACKWARD);
motor2.run(FORWARD);
delay(300);
}else{
lcd.setCursor(0,1);
lcd.print(" ^^^Forward^^^ ");
motor.run(FORWARD);
motor2.run(FORWARD);
count = 1;
}
}
}
if(count == 1) {
for(int i = 0; i<5; i++) { // do this 5 times
ping.fire();
int inches = ping.inches(); // where are we???
int cm = ping.centimeters();
int mseconds = ping.microseconds();
Serial.print(inches); // print on the serial monitor
Serial.print(" , ");
Serial.print(cm);
Serial.print(" , ");
Serial.println(mseconds);
motor.run(FORWARD); //Forward
motor2.run(FORWARD);
delay(100);
if(inches <= 6) { // if the Woody Bot is too close, turn the other way
lcd.setCursor(0,1);
lcd.print(" Turn Left ");
motor.run(BACKWARD);
motor2.run(FORWARD);
delay(300);
}else{
lcd.setCursor(0,1);
lcd.print(" Forward ");
motor.run(FORWARD);
motor2.run(FORWARD);
}
}
}
}
This is my rewritten code trying to put as much as I can in functions. I have tried a dozen variations and keep having problems. The Ping is definitely not activating at 6 inches away. It sometimes gets stuck in the turn left loop. It almost appears that the code is slowed way down and delaying the functions. but I have a feeling I am not formatting something correctly.
//Code for the Woody Bot, with Ping Sensor, and LCD2004 readout
//
//Kevin "Woody" Woodyard
//
//Check out http://woodystime.blogspot.com for more info and build guide.
#include <Ping.h>
#include <AFMotor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
Ping ping = Ping(A3); //set which model ping your using
AF_DCMotor motor(1, MOTOR12_64KHZ); //Set motors to right frequency to match your servo.
// create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ);
// create motor #2, 64KHz pwm
void setup() {
Serial.begin(9600); // start serial communication at 9600 baud
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.print(" Woody Bot");// Print a message to the LCD.
lcd.setCursor(0,1);
lcd.print(" Version 1.4"); //Print on line 2 of the lcd
delay(3000);
pinMode(A0, INPUT_PULLUP); // make pin A0 an input with internal pullup
motor.setSpeed(255); // set the speed 0f the servos
motor2.setSpeed(130);
while (digitalRead(A0) == HIGH){ // Pause until the button is pressed
lcd.setCursor(0,1);
lcd.print(" Press Start! ");
}
}
void loop() {
Scan();
int inches = ping.inches(); // where are we???
Forward();
if(inches <= 6) { // if the Woody Bot gets too close...
Left();
}else{
Forward();
}
}
void Scan(){
for(int i = 0; i<5; i++) // do this 5 times
ping.fire();
}
void Forward(){ //Run both motors forward
lcd.setCursor(0,1);
lcd.print(" ^^^Forward^^^ ");
motor.run(FORWARD);
motor2.run(FORWARD);
delay(100);
}
void Left(){ //Run Motors opposite to rotate left.
lcd.setCursor(0,1);
lcd.print(" Turn Left ");
motor.run(BACKWARD);
motor2.run(FORWARD);
delay(300);
}
void Right(){ //Run Motors opposite to rotate right.
lcd.setCursor(0,1);
lcd.print(" Turn Right ");
motor.run(FORWARD);
motor2.run(BACKWARD);
delay(300);
}
Thanks
WOody