Created
December 27, 2019 18:52
-
-
Save hovedguy/305d629898f97bf4f36f800b651b7cd6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| Physical Pixel | |
| An example of using the Arduino board to receive data from the computer. In | |
| this case, the Arduino boards turns on an LED when it receives the character | |
| 'H', and turns off the LED when it receives the character 'L'. | |
| The data can be sent from the Arduino Serial Monitor, or another program like | |
| Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP. | |
| The circuit: | |
| - LED connected from digital pin 13 to ground | |
| created 2006 | |
| by David A. Mellis | |
| modified 30 Aug 2011 | |
| by Tom Igoe and Scott Fitzgerald | |
| This example code is in the public domain. | |
| http://www.arduino.cc/en/Tutorial/PhysicalPixel | |
| */ | |
| const int ledPin = 13; // the pin that the LED is attached to | |
| const int ledPin2 = 12; // the pin that the LED is attached to | |
| int incomingByte; // a variable to read incoming serial data into | |
| void setup() { | |
| // initialize serial communication: | |
| Serial.begin(9600); | |
| // initialize the LED pin as an output: | |
| pinMode(ledPin, OUTPUT); | |
| pinMode(ledPin2, OUTPUT); | |
| } | |
| void loop() { | |
| // see if there's incoming serial data: | |
| if (Serial.available() > 0) { | |
| // read the oldest byte in the serial buffer: | |
| incomingByte = Serial.read(); | |
| // if it's a capital H (ASCII 72), turn on the LED: | |
| if (incomingByte == 'H') { | |
| digitalWrite(ledPin, HIGH); | |
| } | |
| if (incomingByte == 'I') { | |
| digitalWrite(ledPin2, HIGH); | |
| } | |
| // if it's an L (ASCII 76) turn off the LED: | |
| if (incomingByte == 'L') { | |
| digitalWrite(ledPin, LOW); | |
| } | |
| if (incomingByte == 'M') { | |
| digitalWrite(ledPin2, LOW); | |
| } | |
| } | |
| } | |
| /* Processing code for this example | |
| // Mouse over serial | |
| // Demonstrates how to send data to the Arduino I/O board, in order to turn ON | |
| // a light if the mouse is over a square and turn it off if the mouse is not. | |
| // created 2003-4 | |
| // based on examples by Casey Reas and Hernando Barragan | |
| // modified 30 Aug 2011 | |
| // by Tom Igoe | |
| // This example code is in the public domain. | |
| import processing.serial.*; | |
| float boxX; | |
| float boxY; | |
| int boxSize = 20; | |
| boolean mouseOverBox = false; | |
| Serial port; | |
| void setup() { | |
| size(200, 200); | |
| boxX = width / 2.0; | |
| boxY = height / 2.0; | |
| rectMode(RADIUS); | |
| // List all the available serial ports in the output pane. | |
| // You will need to choose the port that the Arduino board is connected to | |
| // from this list. The first port in the list is port #0 and the third port | |
| // in the list is port #2. | |
| // if using Processing 2.1 or later, use Serial.printArray() | |
| println(Serial.list()); | |
| // Open the port that the Arduino board is connected to (in this case #0) | |
| // Make sure to open the port at the same speed Arduino is using (9600bps) | |
| port = new Serial(this, Serial.list()[0], 9600); | |
| } | |
| void draw() { | |
| background(0); | |
| // Test if the cursor is over the box | |
| if (mouseX > boxX - boxSize && mouseX < boxX + boxSize && | |
| mouseY > boxY - boxSize && mouseY < boxY + boxSize) { | |
| mouseOverBox = true; | |
| // draw a line around the box and change its color: | |
| stroke(255); | |
| fill(153); | |
| // send an 'H' to indicate mouse is over square: | |
| port.write('H'); | |
| } | |
| else { | |
| // return the box to its inactive state: | |
| stroke(153); | |
| fill(153); | |
| // send an 'L' to turn the LED off: | |
| port.write('L'); | |
| mouseOverBox = false; | |
| } | |
| // Draw the box | |
| rect(boxX, boxY, boxSize, boxSize); | |
| } | |
| */ | |
| /* Max/MSP version 5 patch to run with this example: | |
| ----------begin_max5_patcher---------- | |
| 1672.3oc2ZszaaiCD9ryuBBebQVCQRYao8xhf1cQCPVfBzh8RRQ.sDsM2HSZ | |
| HQmlzh9eu7gjsjsEk7y0oWjiHoHm4aluYHGlueUmtiDuPy5B9Cv8fNc99Uc5 | |
| XZR2Pm726zcF4knDRlYXciDylQ4xtWa6SReQZZ+iSeMiEQR.ej8BM4A9C7OO | |
| kkAlSjQSAYTdbFfvA27o2c6sfO.Doqd6NfXgDHmRUCKkolg4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| |
| .P0K+3peBt3NskC | |
| -----------end_max5_patcher----------- | |
| */ |
This file contains hidden or 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
| import serial | |
| import time | |
| # Define the serial port and baud rate. | |
| ser = serial.Serial('COM7', 9600) | |
| time.sleep(1) # wait for the serial connection to initialize | |
| def led_on_off(): | |
| user_input = input("\nInput: n1=LED1_ON, f1=LED1_OFF, n2=LED2_ON, f2_LED2_OFF, o=Both_LEDs_OFF or e=Exit: ") | |
| # LED 1 | |
| if user_input == "n1": | |
| print("LED1 - ON") | |
| time.sleep(0.1) | |
| ser.write(b'H') | |
| led_on_off() | |
| elif user_input == "f1": | |
| print("LED1 - OFF") | |
| time.sleep(0.1) | |
| ser.write(b'L') | |
| led_on_off() | |
| # LED 2 | |
| elif user_input == "n2": | |
| print("LED2 - ON") | |
| time.sleep(0.1) | |
| ser.write(b'I') | |
| led_on_off() | |
| elif user_input == "f2": | |
| print("LED2 - OFF") | |
| time.sleep(0.1) | |
| ser.write(b'M') | |
| led_on_off() | |
| # both LEDs | |
| elif user_input == "o": | |
| print("Both LEDs - OFF") | |
| time.sleep(0.1) | |
| ser.write(b'L') | |
| time.sleep(0.1) | |
| ser.write(b'M') | |
| led_on_off() | |
| # exit program | |
| elif user_input == "e": | |
| print("Program Exiting") | |
| time.sleep(0.1) | |
| ser.write(b'L') | |
| time.sleep(0.1) | |
| ser.write(b'M') | |
| ser.close() | |
| else: | |
| print("Invalid input") | |
| led_on_off() | |
| led_on_off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment