Skip to content

Instantly share code, notes, and snippets.

@hanjae-jea
Last active July 26, 2018 02:59
Show Gist options
  • Save hanjae-jea/6a2ed4cf1d10c562a869edccae61bdf9 to your computer and use it in GitHub Desktop.
Save hanjae-jea/6a2ed4cf1d10c562a869edccae61bdf9 to your computer and use it in GitHub Desktop.
import serial #Serial imported for Serial communication
import time #Required to use delay functions
ArduinoSerial = serial.Serial('/dev/cu.usbmodem1421',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established
print (ArduinoSerial.readline()) #read the serial data and print it as line
print ("Enter 1 to turn ON LED and 0 to turn OFF LED")
while True: #Do this forever
var = input() #get input from user
print ("you entered", var) #print the intput for confirmation
if var == '1': #if the value is 1
ArduinoSerial.write(b'1') #send 1
print ("LED turned ON")
time.sleep(1)
if var == '0': #if the value is 0
ArduinoSerial.write(b'0') #send 0
print ("LED turned OFF")
time.sleep(1)
void setup() {
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(2, OUTPUT); //make the LED pin (13) as output
digitalWrite (2, LOW);
Serial.println("Hi!, I am Arduino");
}
void loop() {
while (Serial.available()){
data = Serial.read();
}
if (data == '1')
digitalWrite (2, HIGH);
else if (data == '0')
digitalWrite (2, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment