#include <LiquidCrystal.h>
#include <Servo.h>
//SETUP
int wartezeit = 100;
int schwellwert = 70;
int Vert_mitte = 90;
int Hori_mitte = 90;
int schrittweitehori = 2;
int calibrate1 = 0;
int calibrate2 = 0;
int calibrate3 = 0;
int horizontalwert=0;
int hori=0;
int vert=-30;
int merke=0;
int stufe=0;
int mitte_alt=0;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
int rssilinks = A2;
int rssirechts = A1;
int backLight = 13;
#define SPKR 9
int links = 0;
int mitte = 0;
int rechts = 0;
int rssiDiv = 0;
int i=0;
int y=0;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
pinMode (backLight, OUTPUT);
digitalWrite (backLight,HIGH);
myservo1.attach(11); // attaches the servo on pin 10 to the servo object
myservo1.write(Hori_mitte);
myservo2.attach(10); // attaches the servo on pin 11 to the servo object
myservo2.write(Vert_mitte);
y=Hori_mitte;
Serial.begin(9600);
for(i=0;i<10;i++)
{
calibrate1=calibrate1+analogRead(rssilinks);
delay(25);
}
calibrate1=calibrate1/10;
for(i=0;i<10;i++)
{
calibrate3=calibrate3+analogRead(rssirechts);
delay(25);
}
calibrate3=calibrate3/10;
pinMode(SPKR, OUTPUT); //set the speaker as output
digitalWrite(SPKR, HIGH);
delay(500);
digitalWrite(SPKR, LOW);
delay(2500);
}
void loop()
{
// Map values to defined range
//links = map(analogRead(rssilinks), 0, calibrate1, 0, 1000);
//mitte = map(analogRead(rssimitte), 0, calibrate2, 0, 1000);
//rechts = map(analogRead(rssirechts), 0, calibrate3, 0, 1000);
links = map(analogRead(rssilinks), 480, 70, 0, 100);
rechts = map(analogRead(rssirechts), 480, 70, 0, 100);
if (links>100)
{
links = 100;
}
if (rechts>100)
{
rechts = 100;
}
Serial.println(links);
Serial.println(rechts);
Serial.println(" ");
//Display rssi values
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(links);
lcd.setCursor(0, 1);
lcd.print(rechts);
rssiDiv = (links - rechts);
if (rssiDiv >= 20 || rssiDiv <= -20)
{
schrittweitehori = 15;
}
else
{
schrittweitehori = 4;
}
if (links > rechts)
{
horizontalwert = horizontalwert + schrittweitehori;
Serial.println("nacch links");
}
if (rechts > links)
{
horizontalwert = horizontalwert - schrittweitehori;
Serial.println("nacch rechts");
}
hori = horizontalwert + Hori_mitte;
if (hori>179)
{
horizontalwert=0;
Serial.println("zurück");
}
if (hori<=0)
{
horizontalwert=0;
Serial.println("zurück");
}
myservo1.write(hori);
myservo2.write(Vert_mitte);
delay(wartezeit);
{
if (rechts = links)
{
mitte_alt=0;
for(merke=1; merke < 5; merke++)
{
mitte = map((analogRead(rssirechts)+analogRead(rssilinks))/2, 0, calibrate2, 0, 100);
if (mitte_alt > mitte)
{
vert = vert+1;
}
if (mitte_alt < mitte)
{
vert = vert-1;
delay(wartezeit);
}
myservo2.write(Vert_mitte + vert);
delay(wartezeit);
mitte_alt=mitte;
}
delay(wartezeit);
}
}
}