#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Wire.h>
void(* resetFunc) (void) = 0;
#define switch1 7 // switch different stations for calibration
#define switch2 8 // change between calib and stations
#define switch3 9 // main station
#define switch4 10 // station 1
#define switch5 11 // station 2
#define switch6 12 // station 3
// led connections
#define led1 22 // main
#define led2 23 // station 1
#define led3 24 // station 2
#define led4 25 //station 3
// relays connection
// PORT
#define led5 28 //left1
#define led6 29 //right1
#define led7 30 //up1
#define led8 31 //down1
// STBD
#define led9 32 //left2
#define led10 33 //right2
#define led11 34 //up2
#define led12 35 //down2
#define sensorPin8 A8
#define sensorPin9 A9
//
float sensorValue8 = 0;
int sensorMin8 = 1023;
int sensorMax8 = 0;
//
float sensorValue9 = 0;
int sensorMin9 = 1023;
int sensorMax9 = 0;
//
int Center8 = 0;
int Left8 = 0 ;
int Right8 = 0 ;
int Center9 = 0;
int Left9 = 0 ;
int Right9 = 0 ;
//
int center8 = 0;
int left8 = 0 ;
int right8 = 0 ;
int center9 = 0;
int left9 = 0 ;
int right9 = 0 ;
// button 1 change calibration
bool buttonstate1;
int num = 0;
// button 2 change between calib and stations
int buttonReading2;
int state2 = LOW;
int previous2 = LOW;
// button 3 Main station
int buttonReading3;
int state3 = LOW;
int previous3 = LOW;
// button 4 Staion 1
int buttonReading4;
int state4 = LOW;
int previous4 = LOW;
// button 5 station 2
int buttonReading5;
int state5 = LOW;
int previous5 = LOW;
// button 6 station 3
int buttonReading6;
int state6 = LOW;
int previous6 = LOW;
// flags for each station
bool flag_1 = 0;
bool flag_2 = 0;
bool flag_3 = 0;
bool flag_m = 0;
bool flag_erase = 0;
bool up_1 = 0;
bool down_1 = 0;
bool left_1 = 0;
bool right_1 = 0;
bool up_right_1 = 0;
bool up_left_1 = 0;
bool down_left_1 = 0;
bool down_right_1 = 0;
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Main station
int xPin = A1;
int yPin = A0;
int xVal, yVal;
int xMin = 1023, xMax = 0, yMin = 1023, yMax = 0;
// station 1
int xPin1 = A3;
int yPin1 = A2;
int xVal1, yVal1;
int xMin1 = 1023, xMax1 = 0, yMin1 = 1023, yMax1 = 0;
//station 2
int xPin2 = A5;
int yPin2 = A4;
int xVal2, yVal2;
int xMin2 = 1023, xMax2 = 0, yMin2 = 1023, yMax2 = 0;
// station 3
int xPin3 = A7;
int yPin3 = A6;
int xVal3, yVal3;
int xMin3 = 1023, xMax3 = 0, yMin3 = 1023, yMax3 = 0;
// EEPROM varaibles
int XMin = 0, XMax = 0, XMin1 = 0, XMax1 = 0, XMin2 = 0, XMax2 = 0, XMin3 = 0, XMax3 = 0;
int YMin = 0, YMax = 0, YMin1 = 0, YMax1 = 0, YMin2 = 0, YMax2 = 0, YMin3 = 0, YMax3 = 0;
//const char* directions[] = {"Left-Up", "Up", "Right-Up", "Left", "Center", "Right", "Left-Down", "Down", "Right-Down"};
const char* directions[] = {"Right-Up", "Right", "Right-Down", "Up", "Center", "Down", "Left-Up", "Left", "Left-Down"};
String direct = " ";
void setup()
{
lcd.init();
lcd.backlight();
lcd.begin (20, 4);
Serial.begin(9600);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
pinMode(switch3, INPUT_PULLUP);
pinMode(switch4, INPUT_PULLUP);
pinMode(switch5, INPUT_PULLUP);
pinMode(switch6, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
pinMode(led10, OUTPUT);
pinMode(led11, OUTPUT);
pinMode(led12, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
digitalWrite(led8, HIGH);
digitalWrite(led9, HIGH);
digitalWrite(led10, HIGH);
digitalWrite(led11, HIGH);
digitalWrite(led12, HIGH);
//main joystick variables
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
//station1
pinMode(xPin1, INPUT);
pinMode(yPin1, INPUT);
//station2
pinMode(xPin2, INPUT);
pinMode(yPin2, INPUT);
//station3
pinMode(xPin3, INPUT);
pinMode(yPin3, INPUT);
loadeeprom();
lcd.setCursor (6, 0);
lcd.print ("Please");
lcd.setCursor (6, 1);
lcd.print ("Select");
lcd.setCursor (7, 2);
lcd.print ("Station");
}
void eraseeeprom()
{
EEPROM.put(0, 0);
delay(10);
EEPROM.put(10, 0);
delay(10);
EEPROM.put(20, 0);
delay(10);
EEPROM.put(30, 0);
delay(10);
EEPROM.put(40, 0);
delay(10);
EEPROM.put(50, 0);
delay(10);
EEPROM.put(60, 0);
delay(10);
EEPROM.put(70, 0);
delay(10);
EEPROM.put(80, 0);
delay(10);
EEPROM.put(90, 0);
delay(10);
EEPROM.put(100, 0);
delay(10);
EEPROM.put(110, 0);
delay(10);
EEPROM.put(120, 0);
delay(10);
EEPROM.put(130, 0);
delay(10);
EEPROM.put(140, 0);
delay(10);
EEPROM.put(150, 0);
delay(10);
EEPROM.put(160, 0);
delay(10);
EEPROM.put(170, 0);
delay(10);
EEPROM.put(180, 0);
delay(10);
EEPROM.put(190, 0);
delay(10);
EEPROM.put(200, 0);
delay(10);
EEPROM.put(210, 0);
delay(10);
}
void loadeeprom()
{
EEPROM.get(0, XMin);
delay(10);
EEPROM.get(10, YMin);
delay(10);
EEPROM.get(20, XMax);
delay(10);
EEPROM.get(30, YMax);
delay(10);
EEPROM.get(40, XMin1);
delay(10);
EEPROM.get(50, YMin1);
delay(10);
EEPROM.get(60, XMax1);
delay(10);
EEPROM.get(70, YMax1);
delay(10);
EEPROM.get(80, XMin2);
delay(10);
EEPROM.get(90, YMin2);
delay(10);
EEPROM.get(100, XMax2);
delay(10);
EEPROM.get(110, YMax2);
delay(10);
EEPROM.get(120, XMin3);
delay(10);
EEPROM.get(130, YMin3);
delay(10);
EEPROM.get(140, XMax3);
delay(10);
EEPROM.get(150, YMax3);
delay(10);
EEPROM.get(160, Center8);
delay(10);
EEPROM.get(170, Left8);
delay(10);
EEPROM.get(180, Right8);
delay(10);
EEPROM.get(190, Center9);
delay(10);
EEPROM.get(200, Left9);
delay(10);
EEPROM.get(210, Right9);
delay(10);
}
void callibpnf()
{
delay(100);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (5, 1);
lcd.print ("Port Deflector");
lcd.setCursor (7, 2);
lcd.print ("Move");
lcd.setCursor (4, 3);
lcd.print ("Up to Down");
sensorValue8 = analogRead(sensorPin8);
if (sensorValue8 > sensorMax8)
{
sensorMax8 = sensorValue8;
}
if (sensorValue8 < sensorMin8)
{
sensorMin8 = sensorValue8;
}
left8 = center8 - sensorMin8;
right8 = sensorMax8 - center8;
Serial.print("Center :");
Serial.print(center8);
Serial.print("Left :");
Serial.print(left8);
Serial.print("Right :");
Serial.print(right8);
Serial.println("");
EEPROM.put(160, center8);
delay(10);
EEPROM.put(170, left8);
delay(10);
EEPROM.put(180, right8);
delay(10);
}
void callibstbdnoz()
{
delay(100);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (5, 1);
lcd.print ("stbd Deflector");
lcd.setCursor (7, 2);
lcd.print ("Move"); //and another load of these from A0 to A10
lcd.setCursor (3, 3);
lcd.print ("Up to Down");
sensorValue9 = analogRead(sensorPin9);
if (sensorValue9 > sensorMax9)
{
sensorMax9 = sensorValue9;
}
if (sensorValue9 < sensorMin9)
{
sensorMin9 = sensorValue9;
}
left9 = center9 - sensorMin9;
right9 = sensorMax9 - center9;
Serial.print("Center :");
Serial.print(center9);
Serial.print("Left :");
Serial.print(left9);
Serial.print("Right :");
Serial.print(right9);
Serial.println("");
EEPROM.put(190, center9);
delay(10);
EEPROM.put(200, left9);
delay(10);
EEPROM.put(210, right9);
delay(10);
}
void joycalabmin()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 1");
lcd.setCursor (7, 2);
lcd.print ("Place it in"); //and another load of these from A0 to A10
lcd.setCursor (1, 3);
lcd.print ("Top Right Corner");
xVal = analogRead(xPin);
yVal = analogRead(yPin);
if (xVal < xMin)
{
xMin = xVal;
}
if (yVal < yMin)
{
yMin = yVal;
}
EEPROM.put(0, xMin);
delay(10);
EEPROM.put(10, yMin);
delay(10);
Serial.print("X Min: ");
Serial.print(xMin);
Serial.print("\tY Min: ");
Serial.print(yMin);
Serial.println(" ");
}
void joycalabmax()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 1");
lcd.setCursor (7, 2);
lcd.print ("Place it in");
lcd.setCursor (1, 3);
lcd.print ("Bottom Left Corner");
xVal = analogRead(xPin);
yVal = analogRead(yPin);
if (xVal > xMax)
{
xMax = xVal;
}
if (yVal > yMax)
{
yMax = yVal;
}
EEPROM.put(20, xMax);
delay(10);
EEPROM.put(30, yMax);
delay(10);
Serial.print("\tX Max: ");
Serial.print(xMax);
Serial.print("\tY Max: ");
Serial.println(yMax);
Serial.println(" ");
}
void joycalabmin1()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 2");
lcd.setCursor (7, 2);
lcd.print ("Place it in"); //and another load of these from A0 to A10
lcd.setCursor (1, 3);
lcd.print ("Top Right Corner");
xVal1 = analogRead(xPin1);
yVal1 = analogRead(yPin1);
if (xVal1 < xMin1)
{
xMin1 = xVal1;
}
if (yVal1 < yMin1)
{
yMin1 = yVal1;
}
EEPROM.put(40, xMin1);
delay(10);
EEPROM.put(50, yMin1);
delay(10);
Serial.print("X Min: ");
Serial.print(xMin1);
Serial.print("\tY Min: ");
Serial.print(yMin1);
Serial.println(" ");
}
void joycalabmax1()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 2");
lcd.setCursor (7, 2);
lcd.print ("Place it in");
lcd.setCursor (1, 3);
lcd.print ("Bottom Left Corner");
xVal1 = analogRead(xPin1);
yVal1 = analogRead(yPin1);
if (xVal1 > xMax1)
{
xMax1 = xVal1;
}
if (yVal1 > yMax1)
{
yMax1 = yVal1;
}
EEPROM.put(60, xMax1);
delay(10);
EEPROM.put(70, yMax1);
delay(10);
Serial.print("\tX Max: ");
Serial.print(xMax1);
Serial.print("\tY Max: ");
Serial.println(yMax1);
Serial.println(" ");
}
void joycalabmin2()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 3");
lcd.setCursor (7, 2);
lcd.print ("Place it in"); //and another load of these from A0 to A10
lcd.setCursor (1, 3);
lcd.print ("Top Right Corner");
xVal2 = analogRead(xPin2);
yVal2 = analogRead(yPin2);
if (xVal2 < xMin2)
{
xMin2 = xVal2;
}
if (yVal2 < yMin2)
{
yMin2 = yVal2;
}
EEPROM.put(80, xMin2);
delay(10);
EEPROM.put(90, yMin2);
delay(10);
Serial.print("X Min: ");
Serial.print(xMin2);
Serial.print("\tY Min: ");
Serial.print(yMin2);
Serial.println(" ");
}
void joycalabmax2()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 3");
lcd.setCursor (7, 2);
lcd.print ("Place it in");
lcd.setCursor (1, 3);
lcd.print ("Bottom Left Corner");
xVal2 = analogRead(xPin2);
yVal2 = analogRead(yPin2);
if (xVal2 > xMax2)
{
xMax2 = xVal2;
}
if (yVal2 > yMax2)
{
yMax2 = yVal2;
}
EEPROM.put(100, xMax2);
delay(10);
EEPROM.put(110, yMax2);
delay(10);
Serial.print("\tX Max: ");
Serial.print(xMax);
Serial.print("\tY Max: ");
Serial.println(yMax);
Serial.println(" ");
}
void joycalabmin3()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 4");
lcd.setCursor (7, 2);
lcd.print ("Place it in"); //and another load of these from A0 to A10
lcd.setCursor (1, 3);
lcd.print ("Top Right Corner");
xVal3 = analogRead(xPin3);
yVal3 = analogRead(yPin3);
if (xVal3 < xMin3)
{
xMin3 = xVal3;
}
if (yVal3 < yMin3)
{
yMin3 = yVal3;
}
EEPROM.put(120, xMin3);
delay(10);
EEPROM.put(130, yMin3);
delay(10);
Serial.print("X Min: ");
Serial.print(xMin3);
Serial.print("\tY Min: ");
Serial.print(yMin3);
Serial.println(" ");
}
void joycalabmax3()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("JoyStick 4");
lcd.setCursor (7, 2);
lcd.print ("Place it in");
lcd.setCursor (1, 3);
lcd.print ("Bottom Left Corner");
xVal3 = analogRead(xPin3);
yVal3 = analogRead(yPin3);
if (xVal3 > xMax3)
{
xMax3 = xVal3;
}
if (yVal3 > yMax3)
{
yMax3 = yVal3;
}
EEPROM.put(140, xMax3);
delay(10);
EEPROM.put(150, yMax3);
delay(10);
Serial.print("\tX Max: ");
Serial.print(xMax3);
Serial.print("\tY Max: ");
Serial.println(yMax3);
Serial.println(" ");
}
void centersensors()
{
delay(500);
lcd.clear();
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
lcd.setCursor (6, 1);
lcd.print ("Make Sure");
lcd.setCursor (6, 2);
lcd.print ("All Sensors");
lcd.setCursor (4, 3);
lcd.print ("Are centered");
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
center8 = analogRead(sensorPin8);
Serial.print("centers:");
Serial.print( center8);
Serial.print(" ");
delay(10);
center9 = analogRead (sensorPin9);
Serial.print( center9);
Serial.print(" ");
Serial.println(" ");
delay(10);
}
void callib()
{
while (flag_erase == 0)
{
eraseeeprom();
flag_erase = 1;
}
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
buttonstate1 = digitalRead(switch1);
if (buttonstate1 == LOW)
{
if (num < 13 )
{
num++;
}
else
{
num = 0;
}
}
switch (num)
{
case 1 :
{
centersensors();
break;
}
case 2 :
{
joycalabmin();
break;
}
case 3:
{
joycalabmax();
break;
}
case 4 :
{
joycalabmin1();
break;
}
case 5:
{
joycalabmax1();
break;
}
case 6 :
{
joycalabmin2();
break;
}
case 7:
{
joycalabmax2();
break;
}
case 8 :
{
joycalabmin3();
break;
}
case 9:
{
joycalabmax3();
break;
}
case 10:
{
callibpnf();
break;
}
case 11:
{
callibstbdnoz();
break;
}
case 12:
{
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Resetting");
delay(1000);
resetFunc();
break;
}
default:
{
lcd.setCursor (2, 0);
lcd.print ("Calibration mode");
delay(100);
lcd.clear();
break;
}
}
}
String getDirection(int x, int y)
{
if (x < 25 && y < 25)
{
return directions[0];
}
else if (x > 75 && y < 25)
{
return directions[2];
}
else if (x < 25 && y > 75)
{
return directions[6];
}
else if (x > 75 && y > 75)
{
return directions[8];
}
else if (x < 25)
{
return directions[3];
}
else if (x > 75)
{
return directions[5];
} else if (y < 25)
{
return directions[1];
} else if (y > 75)
{
return directions[7];
} else
{
return directions[4];
}
}
void stationmain()
{
buttonReading3 = digitalRead(switch3);
if (buttonReading3 == LOW && previous3 == HIGH )
{
flag_m = 1;
if (state3 == LOW)
{
state3 = HIGH;
}
else
{
state3 = LOW;
}
}
previous3 = buttonReading3;
if (state3 == HIGH && flag_m == 1)
{
delay(100);
lcd.clear();
flag_2 = 0;
flag_3 = 0;
flag_1 = 0;
state4 = LOW;
state5 = LOW;
state6 = LOW;
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
xVal = map(analogRead(xPin), XMin, XMax, 0, 100);
yVal = map(analogRead(yPin), YMin, YMax, 0, 100);
//
sensorValue8 = analogRead(sensorPin8);
sensorValue9 = analogRead(sensorPin9);
///////
if (sensorValue8 == Center8)
{ //new
sensorValue8 = 128; //new
}
else
{
sensorValue8 = int(sensorValue8 < Center8 ? ((sensorValue8 - Center8) * 128) / Left8 + 128 : (((sensorValue8 - Center8) * 128) / Right8 + 128)); //new
}
sensorValue8 = constrain(sensorValue8, 0, 255); //new
if (sensorValue9 == Center9)
{
sensorValue9 = 128;
}
else
{
sensorValue9 = int(sensorValue9 < Center9 ? ((sensorValue9 - Center9) * 128) / Left9 + 128 : (((sensorValue9 - Center9) * 128) / Right9 + 128)); //new
}
sensorValue9 = constrain(sensorValue9, 0, 255);
Serial.print("\tXval: ");
Serial.print(xVal);
Serial.print("\tYval: ");
Serial.print(yVal);
Serial.print(" ");
// Print the current direction of the joystick
Serial.print(getDirection(xVal, yVal));
Serial.println(" ");
lcd.setCursor (3, 0);
lcd.print(" Main Station");
lcd.setCursor (6, 1);
lcd.print("Operational");
lcd.setCursor (0, 2);
lcd.print("Xval:");
lcd.setCursor (6, 2);
lcd.print(xVal);
lcd.setCursor (12, 2);
lcd.print("Yval:");
lcd.setCursor (17, 2);
lcd.print(yVal);
lcd.setCursor (8, 3);
lcd.print(getDirection(xVal, yVal));
direct = getDirection(xVal, yVal);
if (direct == "Up")
{
digitalWrite(led7, LOW);
delay(20);
digitalWrite(led8, HIGH);
delay(20);
digitalWrite(led11, LOW);
delay(20);
digitalWrite(led12, HIGH);
delay(20);
if (sensorValue8 > 250)
{
digitalWrite(led7, HIGH);
delay(20);
}
if (sensorValue9 > 250)
{
digitalWrite(led11, HIGH);
delay(20);
}
}
if (direct == "Down")
{
digitalWrite(led7, HIGH);
delay(20);
digitalWrite(led8, LOW);
delay(20);
digitalWrite(led11, HIGH);
delay(20);
digitalWrite(led12, LOW);
delay(20);
if (sensorValue8 > 250)
{
digitalWrite(led8, HIGH);
delay(20);
}
if (sensorValue9 > 250)
{
digitalWrite(led12, HIGH);
delay(20);
}
}
if (direct == "Left")
{
digitalWrite(led7, HIGH);
delay(20);
digitalWrite(led8, LOW);
delay(20);
digitalWrite(led11, LOW);
delay(20);
digitalWrite(led12, HIGH);
delay(20);
if (sensorValue8 > 250)
{
digitalWrite(led8, HIGH);
delay(20);
}
if (sensorValue9 < 0)
{
digitalWrite(led11, HIGH);
delay(20);
}
}
if (direct == "Right")
{
digitalWrite(led7, LOW);
delay(20);
digitalWrite(led8, HIGH);
delay(20);
digitalWrite(led11, HIGH);
delay(20);
digitalWrite(led12, LOW);
delay(20);
if (sensorValue8 < 10)
{
digitalWrite(led7, HIGH);
delay(20);
}
if (sensorValue9 > 250)
{
digitalWrite(led12, HIGH);
delay(20);
}
}
if (direct == "Center")
{
if ( sensorValue8 > 128)
{
if (sensorValue8 > 128 && sensorValue8 < 136)
{
digitalWrite(led7, HIGH);
}
else
{
digitalWrite(led7, LOW);
}
}
if (sensorValue8 < 128)
{
if (sensorValue8 < 128 && sensorValue8 > 120 )
{
digitalWrite(led8, HIGH);
}
else
{
digitalWrite(led8, LOW);
}
}
if ( sensorValue9 > 128)
{
if (sensorValue9 > 128 && sensorValue9 < 136)
{
digitalWrite(led11, HIGH);
}
else
{
digitalWrite(led11, LOW);
}
}
if (sensorValue9 < 128)
{
if (sensorValue9 < 128 && sensorValue9 > 120)
{
digitalWrite(led12, HIGH);
}
else
{
digitalWrite(led12, LOW);
}
}
}
if (direct == "Left-Up")
{
if ( sensorValue8 > 128)
{
if (sensorValue8 > 128 && sensorValue8 < 136)
{
digitalWrite(led7, HIGH);
}
else
{
digitalWrite(led7, LOW);
}
}
if (sensorValue8 < 128)
{
if (sensorValue8 < 128 && sensorValue8 > 120 )
{
digitalWrite(led8, HIGH);
}
else
{
digitalWrite(led8, LOW);
}
}
if ( sensorValue9 < 10)
{
digitalWrite(led11, HIGH);
}
else
{
digitalWrite(led11, LOW);
}
}
if (direct == "Right-Up" )
{
if ( sensorValue8 < 10)
{
digitalWrite(led7, HIGH);
}
else
{
digitalWrite(led7, LOW);
}
if ( sensorValue9 > 128)
{
if (sensorValue9 > 128 && sensorValue9 < 136)
{
digitalWrite(led11, HIGH);
}
else
{
digitalWrite(led11, LOW);
}
}
if (sensorValue9 < 128)
{
if (sensorValue9 < 128 && sensorValue9 > 120 )
{
digitalWrite(led12, HIGH);
}
else
{
digitalWrite(led12, LOW);
}
}
}
if (direct == "Right-Down")
{
if ( sensorValue8 > 128)
{
if (sensorValue8 > 128 && sensorValue8 < 136)
{
digitalWrite(led7, HIGH);
}
else
{
digitalWrite(led7, LOW);
}
}
if (sensorValue8 < 128)
{
if (sensorValue8 < 128 && sensorValue8 > 120 )
{
digitalWrite(led8, HIGH);
}
else
{
digitalWrite(led8, LOW);
}
}
if (sensorValue9 > 250)
{
digitalWrite(led12, HIGH);
}
else
{
digitalWrite(led12, LOW);
}
}
if (direct == "Left-Down" )
{
if ( sensorValue8 > 250)
{
digitalWrite(led8, HIGH);
}
else
{
digitalWrite(led8, LOW);
}
if ( sensorValue9 > 128)
{
if (sensorValue9 > 128 && sensorValue9 < 136)
{
digitalWrite(led11, HIGH);
}
else
{
digitalWrite(led11, LOW);
}
}
if (sensorValue9 < 128)
{
if (sensorValue9 < 128 && sensorValue9 > 120 )
{
digitalWrite(led12, HIGH);
}
else
{
digitalWrite(led12, LOW);
}
}
}
}
else
{
state3 = HIGH;
}
}
void station1()
{
buttonReading4 = digitalRead(switch4);
if (buttonReading4 == LOW && previous4 == HIGH )
{
flag_1 = 1;
if (state4 == LOW)
{
state4 = HIGH;
}
else
{
state4 = LOW;
}
}
previous4 = buttonReading4;
if (state4 == HIGH && flag_1 == 1)
{
flag_2 = 0;
flag_3 = 0;
flag_m = 0;
delay(50);
lcd.clear();
xVal1 = map(analogRead(xPin1), XMin1, XMax1, 0, 100);
yVal1 = map(analogRead(yPin1), YMin1, YMax1, 0, 100);
Serial.print("\tXval: ");
Serial.print(xVal1);
Serial.print("\tYval: ");
Serial.print(yVal1);
Serial.print(" ");
// Print the current direction of the joystick
Serial.print(getDirection(xVal1, yVal1));
Serial.println(" ");
state3 = LOW;
state5 = LOW;
state6 = LOW;
digitalWrite(led1, LOW );
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
lcd.setCursor (3, 0);
lcd.print(" Station 1");
lcd.setCursor (6, 1);
lcd.print("Operational");
lcd.setCursor (0, 2);
lcd.print("Xval:");
lcd.setCursor (6, 2);
lcd.print(xVal1);
lcd.setCursor (12, 2);
lcd.print("Yval:");
lcd.setCursor (17, 2);
lcd.print(yVal1);
lcd.setCursor (8, 3);
lcd.print(getDirection(xVal1, yVal1));
direct = getDirection(xVal1, yVal1);
}
else
{
state4 = HIGH;
}
}
void station2()
{
buttonReading5 = digitalRead(switch5);
if (buttonReading5 == LOW && previous5 == HIGH )
{
flag_2 = 1;
if (state5 == LOW)
{
state5 = HIGH;
}
else
{
state5 = LOW;
}
}
previous5 = buttonReading5;
if (state5 == HIGH && flag_2 == 1)
{
flag_1 = 0;
flag_3 = 0;
flag_m = 0;
delay(50);
lcd.clear();
xVal2 = map(analogRead(xPin2), XMin2, XMax2, 0, 100);
yVal2 = map(analogRead(yPin2), YMin2, YMax2, 0, 100);
Serial.print("\tXval: ");
Serial.print(xVal2);
Serial.print("\tYval: ");
Serial.print(yVal2);
Serial.print(" ");
// Print the current direction of the joystick
Serial.print(getDirection(xVal2, yVal2));
Serial.println(" ");
state3 = LOW;
state4 = LOW;
state6 = LOW;
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
lcd.setCursor (3, 0);
lcd.print(" Station 2");
lcd.setCursor (6, 1);
lcd.print("Operational");
lcd.setCursor (0, 2);
lcd.print("Xval:");
lcd.setCursor (6, 2);
lcd.print(xVal2);
lcd.setCursor (12, 2);
lcd.print("Yval:");
lcd.setCursor (17, 2);
lcd.print(yVal2);
lcd.setCursor (8, 3);
lcd.print(getDirection(xVal2, yVal2));
direct = getDirection(xVal2, yVal2);
}
else
{
state5 = HIGH;
}
}
void station3()
{
buttonReading6 = digitalRead(switch6);
if (buttonReading6 == LOW && previous6 == HIGH)
{
flag_3 = 1;
if (state6 == LOW)
{
state6 = HIGH;
}
else
{
state6 = LOW;
}
}
previous6 = buttonReading6;
if (state6 == HIGH && flag_3 == 1)
{
flag_1 = 0;
flag_2 = 0;
flag_m = 0;
delay(50);
lcd.clear();
xVal3 = map(analogRead(xPin3), XMin3, XMax3, 0, 100);
yVal3 = map(analogRead(yPin3), YMin3, YMax3, 0, 100);
Serial.print("\tXval: ");
Serial.print(xVal3);
Serial.print("\tYval: ");
Serial.print(yVal3);
Serial.print(" ");
// Print the current direction of the joystick
Serial.print(getDirection(xVal3, yVal3));
Serial.println(" ");
state3 = LOW;
state4 = LOW;
state6 = LOW;
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
lcd.setCursor (3, 0);
lcd.print(" Station 3");
lcd.setCursor (6, 1);
lcd.print("Operational");
lcd.setCursor (0, 2);
lcd.print("Xval:");
lcd.setCursor (6, 2);
lcd.print(xVal3);
lcd.setCursor (12, 2);
lcd.print("Yval:");
lcd.setCursor (17, 2);
lcd.print(yVal3);
lcd.setCursor (8, 3);
lcd.print(getDirection(xVal3, yVal3));
direct = getDirection(xVal3, yVal3);
}
else
{
state6 = HIGH;
}
}
void loop()
{
buttonReading2 = digitalRead(switch2);
if (buttonReading2 == LOW && previous2 == HIGH)
{
if (state2 == LOW)
{
state2 = HIGH;
}
else
{
state2 = LOW;
}
}
previous2 = buttonReading2;
if (state2 == HIGH)
{
callib();
}
else
{
stationmain();
station1();
station2();
station3();
}
}