Stap 11: Programma/Sketch
#include
#include
LiquidCrystal LCD(10,9,5,4,3,2);
int i;
char j;
int redled = 0;
int blueled = 1;
int trigPin = 12;
int echoPin = 13;
int left1motor = 6;
int left2motor = 7;
int right1motor = 8;
int right2motor = 11;
float pingTime;
float targetDistance;
VOID Setup {}
Serial.begin(9600);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode (left1motor, OUTPUT);
pinMode (left2motor, OUTPUT);
pinMode (right1motor, OUTPUT);
pinMode (right2motor, OUTPUT);
pinMode (redled, OUTPUT);
pinMode (blueled, OUTPUT);
LCD.begin(16,2);
LCD_start();
terwijl (!. Serial.available());
j=Serial.Read();
}
void loop {}
Als (j == 'a' || j == 'A')
{j = 'Z';
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem1] = {'A', 'U','T ', 'O,' ben ', 'A','T ', 'I', 'C', '','C ','T ', 'R', 'L'};
char [displayitem2] = {'A', 'C','T ', 'I', 'V', 'A','T ', 'E','D '};
voor (ik = 0; ik < 14; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem1[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 9; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem2[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(9,1);
LCD.print("");
delay(500);
LCD.setCursor(9,1);
LCD.print("_");
}
Automatic();
}
anders als (j == ben ' || j == ben ')
{
j = 'Z';
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem3] = {ben ', "A'," n ", 'U', 'A', 'L', '',' C',"O', "n",'T ', 'R', 'O', 'L'};
char [displayitem4] = {'A', 'C','T ', 'I', 'V', 'A','T ', 'E','D '};
voor (ik = 0; ik < 14; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem3[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 9; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem4[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(9,1);
LCD.print("");
delay(500);
LCD.setCursor(9,1);
LCD.print("_");
}
Manual();
}
anders
{
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem5] = {'W', 'A', 'Ik','T ', 'I', "n", 'G', '',' ', 'F', 'O', 'R'};
char [displayitem6] = {'I', "n", de ','T ', 'R', 'U', 'C','T ', 'I', 'O','N'};
voor (ik = 0; ik < 12; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem5[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 11; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem6[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(11,1);
LCD.print("");
delay(500);
LCD.setCursor(11,1);
LCD.print("_");
}
}
terwijl (!. Serial.available());
j=Serial.Read();
}
VOID manual()
{
char com;
terwijl (!. Serial.available());
g:
com=Serial.Read();
Als (com == 'w' || com == 'W')
{
terwijl (!. Serial.available())
{
digitalWrite (left1motor, hoge);
digitalWrite (left2motor, laag);
digitalWrite (right1motor, hoge);
digitalWrite (right2motor, laag);
LCD_normal();
Serial.println("Forward");
}
}
anders als (com == de ' || com == de ')
{
terwijl (!. Serial.available())
{
digitalWrite (left1motor, laag);
digitalWrite (left2motor, hoge);
digitalWrite (right1motor, laag);
digitalWrite (right2motor, hoge);
LCD_normal();
Serial.println("BACKWARD");
}
}
anders als (com == 'a' || com == 'A')
{
terwijl (!. Serial.available())
{
digitalWrite (left1motor, laag);
digitalWrite (left2motor, hoge);
digitalWrite (right1motor, hoge);
digitalWrite (right2motor, laag);
LCD_obstacle();
Serial.println ("LINKSAF");
}
}
anders als (com == had'|| com == had')
{
terwijl (!. Serial.available())
{
digitalWrite (left1motor, hoge);
digitalWrite (left2motor, laag);
digitalWrite (right1motor, laag);
digitalWrite (right2motor, hoge);
LCD_obstacle();
Serial.println ("RECHTSAF");
}
}
Als (com == 'a' || com == 'A' || com == de ' || com == de ' || com == 'A' || com == 'a' || com == had'|| com == had')
goto g;
anders als (com == 'e' || com == "E")
{
digitalWrite (left1motor, laag);
digitalWrite (left2motor, laag);
digitalWrite (right1motor, laag);
digitalWrite (right2motor, laag);
LCD_final();
Serial.println("STOPPED");
}
}
VOID automatic()
{
g:
terwijl (!. Serial.available())
{
digitalWrite (trigPin, laag);
delayMicroseconds(2);
digitalWrite (trigPin, hoge);
delayMicroseconds(10);
digitalWrite (trigPin, laag);
pingTime = pulseIn (echoPin, hoge);
pingTime = pingTime/29;
targetDistance = pingTime/2;
Serial.Print ("de afstand tot doelstelling is:");
Serial.Print(targetDistance);
Serial.println ("cm");
delay(10);
if(targetDistance==0.0)
{
digitalWrite (left1motor, hoge);
digitalWrite (left2motor, laag);
digitalWrite (right1motor, hoge);
digitalWrite (right2motor, laag);
LCD_normal();
Serial.println("Forward");
}
anders if(targetDistance>30.0)
{
digitalWrite (left1motor, hoge);
digitalWrite (left2motor, laag);
digitalWrite (right1motor, hoge);
digitalWrite (right2motor, laag);
LCD_normal();
Serial.println("Forward");
}
anders if(targetDistance<10.0)
{
digitalWrite (left1motor, laag);
digitalWrite (left2motor, hoge);
digitalWrite (right1motor, laag);
digitalWrite (right2motor, hoge);
LCD_normal();
Serial.println("BACKWARD");
}
anders if(targetDistance<30.0)
{
digitalWrite (left1motor, laag);
digitalWrite (left2motor, hoge);
digitalWrite (right1motor, hoge);
digitalWrite (right2motor, laag);
LCD_obstacle();
Serial.println ("LINKSAF");
}
}
int com=Serial.read();
Als (com! = 'e' || com! = "E")
goto g;
}
VOID LCD_start()
{
int i;
LCD.setCursor(0,0);
LCD.print ("* Welkom SIR *");
delay(1000);
LCD.setCursor(0,1);
LCD.print ("PROJECT: ABRC");
delay(2000);
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem7] = {'C', 'H', 'E', 'C', 'K', 'I', "n", 'G',' ', ben ', 'O','T ', 'O', 'R', de '};
char [displayitem8] = {'C', 'O,' ben ', 'P', 'L', 'E','T ', 'E','D '};
voor (ik = 0; ik < 15; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem7[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(200);
LCD.setCursor(15,0);
LCD.print("_");
delay(200);
LCD.setCursor(15,0);
LCD.print("");
}
voor (ik = 0; ik < 9; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem8[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(9,1);
LCD.print("");
delay(500);
LCD.setCursor(9,1);
LCD.print("_");
}
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem9] = {'B', 'A', de ', 'E', 'C', 'O','D ', 'E'};
char [displayitem10] = {'U', 'P','D ', 'A','T ', 'E','D '};
voor (ik = 0; ik < 8; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem9[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(200);
LCD.setCursor(8,0);
LCD.print("_");
delay(200);
LCD.setCursor(8,0);
LCD.print("");
}
voor (ik = 0; ik < 7; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem10[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(7,1);
LCD.print("");
delay(500);
LCD.setCursor(7,1);
LCD.print("_");
}
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem11] = {'I', "n",'T ', 'I', 'A', 'L', 'I', de ', 'I', "n", 'G'};
char [displayitem12] = {de ', 'E', 'Q', 'U', 'E', "n", "C", "E"};
voor (ik = 0; ik < 11; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem11[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 8; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem12[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(8,1);
LCD.print("");
delay(500);
LCD.setCursor(8,1);
LCD.print("_");
}
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
char [displayitem5] = {'W', 'A', 'Ik','T ', 'I', "n", 'G', '',' ', 'F', 'O', 'R'};
char [displayitem6] = {'I', "n", de ','T ', 'R', 'U', 'C','T ', 'I', 'O','N'};
voor (ik = 0; ik < 12; i ++)
{
LCD.setCursor(i,0);
LCD.print(displayitem5[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 11; i ++)
{
LCD.setCursor(i,1);
LCD.print(displayitem6[i]);
LCD.print("_");
delay(50);
}
voor (ik = 0; ik < 3; i ++)
{
delay(500);
LCD.setCursor(11,1);
LCD.print("");
delay(500);
LCD.setCursor(11,1);
LCD.print("_");
}
}
VOID LCD_normal()
{
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
LCD.setCursor(0,0);
LCD.print("ULTRASONIC");
LCD.setCursor(0,1);
LCD.print("INITIATED");
LCD.setCursor(11,0);
LCD.print(targetDistance);
delay(50);
}
VOID LCD_obstacle()
{
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
LCD.setCursor(0,0);
LCD.print("OBSTACLE");
LCD.setCursor(0,1);
LCD.print("DETECTED");
LCD.setCursor(11,0);
LCD.print(targetDistance);
delay(100);
}
VOID LCD_final()
{
LCD.setCursor(0,0);
LCD.print ("** dank u **");
delay(1000);
LCD.setCursor(0,1);
LCD.print ("PROJECT: ABRC");
delay(2000);
voor (ik = 0; ik < 2; i ++)
{
LCD.setCursor(0,i);
LCD.print("");
}
}