Created
March 11, 2020 06:56
-
-
Save peow2373/1bc3419f3702d213acc087435d1fad11 to your computer and use it in GitHub Desktop.
Arduino code to receive x and y inputs from a joystick which is then communicated to the serial monitor. The code also checks for incoming values from the serial monitor to determine when to turn off the LEDs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These constants won't change. They're used to give names to the pins used: | |
int xAxis = A0; // Analog input pin that the x-axis of the joystick is attached to | |
int yAxis = A1; // Analog input pin that the y-axis of the joystick is attached to | |
int sensorValue1 = 0; // value read from the potentometer | |
int sensorValue2 = 0; // calue read from the photoreceptor | |
int LEDpin1 = 4; | |
int LEDpin2 = 7; | |
int LEDpin3 = 8; | |
int incomingByte; | |
bool life1 = true; | |
bool life2 = true; | |
bool life3 = true; | |
void setup() { | |
// initialize serial communications at 9600 bps: | |
pinMode(LEDpin1, OUTPUT); | |
pinMode(LEDpin2, OUTPUT); | |
pinMode(LEDpin3, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
// read the analog in value: | |
sensorValue1 = analogRead(xAxis); | |
// read the analog in value: | |
sensorValue2 = analogRead(yAxis); | |
// print the analog inputs to the Serial Monitor: | |
Serial.print(sensorValue1); | |
Serial.print("||"); | |
Serial.println(sensorValue2); | |
if (Serial.available() > 0) { | |
incomingByte = Serial.read(); | |
if (incomingByte == 'A') { | |
life1 = false; | |
} else if (incomingByte == 'B'){ | |
life2 = false; | |
} else if (incomingByte == 'C') { | |
life3 = false; | |
} else if (incomingByte == 'D') { | |
life1 = true; | |
life2 = true; | |
life3 = true; | |
} | |
} | |
if (life1) { | |
digitalWrite(LEDpin1, HIGH); | |
} else { | |
digitalWrite(LEDpin1, LOW); | |
} | |
if (life2) { | |
digitalWrite(LEDpin2, HIGH); | |
} else { | |
digitalWrite(LEDpin2, LOW); | |
} | |
if (life3) { | |
digitalWrite(LEDpin3, HIGH); | |
} else { | |
digitalWrite(LEDpin3, LOW); | |
} | |
// wait 2 milliseconds before the next loop for the analog-to-digital | |
// converter to settle after the last reading: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment