Skip to content

Instantly share code, notes, and snippets.

@MatthewScholefield
Forked from jreisstudio/PythonArduino.py
Last active February 9, 2018 06:29
Show Gist options
  • Save MatthewScholefield/06b028c4b542f27a1e789c50c5b67551 to your computer and use it in GitHub Desktop.
Save MatthewScholefield/06b028c4b542f27a1e789c50c5b67551 to your computer and use it in GitHub Desktop.
Python + Arduino on/off the LED.
#define LED 9
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
int brightness = (int)Serial.parseInt();
if (brightness > 255) {
brightness = 255;
else if (brightness < 0) {
brightness = 0;
}
analogWrite(LED, brightness);
}
}
import serial # you need to install the pySerial :pyserial.sourceforge.net
import time
with open('RMS_EMG.txt') as f:
lines = f.read().split('\n')
arduino = serial.Serial('/dev/tty.usbserial', 9600)
for line in lines:
brightness = int(float(line.split(',')[-1]) * 255)
arduino.write(bytes(str(brightness), 'ascii'))
time.sleep(1/100.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment