Hi All,
Perhaps someone can tell me where I'm going wrong here. The code is now almost running as it should. Except for the few lines underlined! I have used Serial.println to confirm that tilt=248 and I can see it on the serial monitor.
I think I'm right with if(tilt=etc) {do it} but what when you need an if inside another as in this case can it be nested?? I'm sure it's something simple that I can't quite get to grips with just yet! But I am trying and the more I do the more I like it.
Here's my code.
Regards
Mel.
/*
*******************************
; Filename: PololuDrive26082013
; Date: 10/08/2013
; File Version: 18082913
; Written by : Mel Saunders
; Function: BuggyDrive
; Last Revision: 26/08/2013
; Target Uno
; *******************************
This program demonstrates the use of an L293 dual H bridge.
This can run 2x DC motors control their speed and direction.
*/
#include <Servo.h>
Servo scanservo; //Ping Sensor Servo
int Left=180, Centre=90, Right=0; //Servo angle in Degrees ??Hextronic servo works back to front LEFT=180, RIGHT=0
const int MA1=0,MA2=1,MB1=2,MB2=3; //Motor pins
const int PWMpin=11; //Enable pin on L293 for PWM
const int scanservopin = A0; // Pin number for scan servo
int distance,cm,FRQ,TD,A,B,speedy;
long int randNumber,motorSpeed;
int tilt;
boolean go;
void setup()
{
pinMode(MA1, OUTPUT); // sets the digital pin as output
pinMode(MA2, OUTPUT); // sets the digital pin as output
pinMode(MB1, OUTPUT); // sets the digital pin as output
pinMode(MB2, OUTPUT); // sets the digital pin as output
pinMode(scanservopin, OUTPUT);
go = true;
scanservo.attach(scanservopin); // Attach the scan servo to pin 11
}
// * * * start of Main program * * * *
void loop()
{
if (go == true)
{
scan();
do
{
distance=scanner(cm);
delay(200);
sound(1600,3);
delay(200);
}
while(distance >20);
startup();
}
distance=scanner(cm);
if(distance <=10)
{
TD=300;
Halt(TD);
BackOff();
TD=500;
Halt(TD);
Radar();
}
if(distance >10 and distance <20)
{
analogWrite(PWMpin,95);
delay(800);
}
if(distance >20)
{
digitalWrite(MA1,HIGH); //Left motor forward
digitalWrite(MA2,LOW);
digitalWrite(MB1,HIGH); //Right motor forward
digitalWrite(MB2,LOW);
randomSeed(analogRead(3));
speedy = random(550,2500); // Speed for turning, reverese, etc.
motorSpeed=random(70,255); //random motor speed
[u][b] tilt=analogRead(A1); //Read analogue switches, TILT, etc.
delay(100);
if(tilt >=245 and tild<=255);
{
(BackOff);[/b][/u]
}
}
}// end Main loop
//------------------------------------------------------------------
void startup()
{
digitalWrite(MA1,HIGH); //Left motor forward
digitalWrite(MA2,LOW);
digitalWrite(MB1,HIGH); //Right motor forward
digitalWrite(MB2,LOW);
for (int i= 40; i <= 255; i++) //start slow, increase speed
{
analogWrite(PWMpin,i);
delay(40);
}
go = false; //Set go to false
}
//-----------------------------------------------------------------
void Halt(int TD) //Both motors STOP
{
digitalWrite(MA1,LOW);
digitalWrite(MA2,LOW);
digitalWrite(MB1,LOW);
digitalWrite(MB2,LOW);
delay(TD);
}
//------------------------------------------------------------------
void Rturn() //Turn right
{
digitalWrite(MA1,HIGH); //Left motor forward
digitalWrite(MA2,LOW);
digitalWrite(MB1,LOW); //Right motor reverse
digitalWrite(MB2,HIGH);
delay(speedy);
speedy = random(250,3000);
}
//------------------------------------------------------------------
void Lturn() //Turn left
{
digitalWrite(MA1,LOW); //Left motor reverse
digitalWrite(MA2,HIGH);
digitalWrite(MB1,HIGH); //Right motor forward
digitalWrite(MB2,LOW);
delay(speedy);
speedy = random(250,3000);
}
//------------------------------------------------------------------
void Forward() //forward
{
digitalWrite(MA1,HIGH); //Left motor forward
digitalWrite(MA2,LOW);
digitalWrite(MB1,HIGH); //Right motor forward
digitalWrite(MB2,LOW);
}
//------------------------------------------------------------------
int Back(int TD) //Reverse
{
analogWrite(PWMpin,motorSpeed);
digitalWrite(MA1,LOW); //Left motor reverse
digitalWrite(MA2,HIGH);
digitalWrite(MB1,LOW); //Right motor reverse
digitalWrite(MB2,HIGH);
delay(speedy);
}
//------------------------------------------------------------------
void BackOff() //back off!
{
analogWrite(PWMpin,motorSpeed);
delay(100);
digitalWrite(MA1,LOW); //Left motor reverse
digitalWrite(MA2,HIGH);
digitalWrite(MB1,LOW); //Right motor reverse
digitalWrite(MB2,HIGH);
delay(speedy);
}
//------------------------------------------------------------------
void sound(int FRQ,int TD)
{
tone(12,FRQ,TD);
}
//------------------------------------------------------------------
void scan()
{
for (int i=Right; i <= Left; i++)
{
scanservo.write(i);
delay(20);
}
scanservo.write(Centre);
}
//------------------------------------------------------------------
long scanner(long cm)
{
const int pingPin=7, EchoPin=8;
long duration;
// 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);
pinMode(EchoPin, INPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
duration = pulseIn(EchoPin, HIGH);
delay(100);
// convert the time into a distance
// inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
delay(100);
return (cm);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
void Radar()
{
scanservo.write(180);
delay(300);
tone(12,1500,30);
A=scanner(cm);
delay(100);
scanservo.write(0);
delay(300);
tone(12,1500,30);
B=scanner(cm);
scanservo.write(90);
delay(300);
if (A>B)
{
Lturn(); //Then turn left
}
else
{
Rturn(); //Then turn right
}
delay(100);
}