Code
Code
#define PIN_ANALOG_Y 1
int x_position;
int y_position;
int x_direction;
int y_direction;
void setup() {
Serial.begin(9600);
void loop() {
x_direction = 0;
y_direction = 0;
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
if (x_direction == -1)
{
if (y_direction == -1) {
Serial.println("left-down");
if(distance<= 10)
{
Serial.print(distance);
Stop(50);
Blink();
}
else
Backword(140,90,20);
} else if (y_direction == 0) {
Serial.println("left");
Turn_Left(100,20);
digitalWrite(Led_L,HIGH);
}
else
{
// y_direction == 1
Serial.println("left-up");
Forword(150,120,10);
}
}
else if (x_direction == 0)
{
if (y_direction == -1)
{
Serial.println("down");
if(distance<= 10)
{
Serial.print(distance);
Stop(50);
Blink();
Buzzer(10);
}
else
{
Backword(150,120,10);
digitalWrite(Led_O,HIGH);
}
}
else if (y_direction == 0)
{
Serial.println("centered");
Stop(250);
}
else
{
// y_direction == 1
Serial.println("Forword");
Forword(140,115,30);
}
}
else
{
// x_direction == 1
if (y_direction == -1)
{
Serial.println("right-down");
if(distance<= 5)
{
Serial.print(distance);
Blink();
Stop(100);
Buzzer(10);
}
else
{
Backword(150,120,10);
digitalWrite(Led_O,HIGH);
}
}
else if (y_direction == 0)
{
Serial.println("right");
Turn_Right(130,20);
digitalWrite(Led_R,HIGH);
}
else
{
// y_direction == 1
Serial.println("right-up");
Forword(150,125,10);
}
}
delay(250);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Forword(int i,int j,int k)
//both motor are forword
{
//lcd.print("FORWORD");
Serial.println("\t \t FORWORD");
delay(k);
}
void Backword(int i,int j,int k)
//both motor are backword
{
Serial.println("\t \t BACKWORD");
delay(k);
}
delay(j);
}
delay(j);
}
void Stop(int k)
//Both motor is stopped
{
Serial.println("\t \t STOP");
void Blink()
{
digitalWrite(Led_O,HIGH);
digitalWrite(Led_O,HIGH);
digitalWrite(Led_O,HIGH);
delay(20);
digitalWrite(Led_O,LOW);
digitalWrite(Led_O,LOW);
digitalWrite(Led_O,LOW);
delay(20);
digitalWrite(Led_O,HIGH);
delay(20);
digitalWrite(Led_O,LOW);
delay(20);
}
void Buzzer(int i) // here i is the frequency in KHz
{
for (int j=0;j<100;j++)
{
long T=1000/i; //frequency converted into Time(cycle) mili seconds
digitalWrite(buzPin,HIGH);
delay(T/2); //half cycle time on
digitalWrite(buzPin,LOW);
delay(T/2); // half cycle time off
}
}