Created
September 6, 2019 21:33
-
-
Save MichalSkoula/6e13b7787e6d0031eb6372f2897aca36 to your computer and use it in GitHub Desktop.
arduino reads serial input from the pc
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
int incomingByte = 0; // for incoming serial data | |
const int ledPinRed = 2; // the number of the LED pin | |
const int ledPinGreen = 3; // the number of the LED pin | |
const int ledPinYellow = 4; | |
const int ledPinBlue = 5; | |
void setup() { | |
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps | |
pinMode(ledPinRed, OUTPUT); | |
pinMode(ledPinGreen, OUTPUT); | |
pinMode(ledPinYellow, OUTPUT); | |
pinMode(ledPinBlue, OUTPUT); | |
} | |
void loop() { | |
// send data only when you receive data: | |
if (Serial.available() > 0) { | |
// read the incoming byte: | |
incomingByte = Serial.read(); | |
// say what you got: | |
Serial.print("I received: "); | |
Serial.println(incomingByte, DEC); | |
switch(incomingByte) { | |
case 114: digitalWrite(ledPinRed, HIGH); delay(500); digitalWrite(ledPinRed, LOW); break; | |
case 103: digitalWrite(ledPinGreen, HIGH); delay(500); digitalWrite(ledPinGreen, LOW); break; | |
case 121: digitalWrite(ledPinYellow, HIGH); delay(500); digitalWrite(ledPinYellow, LOW); break; | |
case 98: digitalWrite(ledPinBlue, HIGH); delay(500); digitalWrite(ledPinBlue, LOW); break; | |
default: break; | |
} | |
delay(300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment