Created
February 3, 2017 17:05
-
-
Save mz2212/4a0699175561c26217f4a26e06597e42 to your computer and use it in GitHub Desktop.
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
#define Blue 9 | |
#define Green 10 | |
#define Red 11 | |
#define DelayTime 5 | |
byte blueLast = 0; | |
byte greenLast = 0; | |
byte redLast = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(Blue, OUTPUT); | |
pinMode(Green, OUTPUT); | |
pinMode(Red, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
byte brightness; | |
byte color; | |
if(Serial.available()){ | |
color = Serial.read(); | |
while(Serial.available() == 0){} | |
brightness = Serial.read(); | |
if(color == 'A'){ fade(Blue, brightness); } | |
if(color == 'B'){ fade(Green, brightness); } | |
if(color == 'C'){ fade(Red, brightness); } | |
} | |
} | |
void fade(int color, byte brightness){ | |
switch(color){ | |
case Blue: | |
changeBrightness(Blue, brightness, blueLast); | |
blueLast = brightness; | |
break; | |
case Green: | |
changeBrightness(Green, brightness, greenLast); | |
greenLast = brightness; | |
break; | |
case Red: | |
changeBrightness(Red, brightness, redLast); | |
redLast = brightness; | |
break; | |
default: | |
break; | |
} | |
} | |
void changeBrightness(int color, byte brightness, byte lastBrightness){ | |
if(brightness >= lastBrightness){ //Turn up the brightness | |
for(int i = lastBrightness; i <= brightness; i++){ | |
analogWrite(color, i); | |
delay(DelayTime); | |
} | |
} | |
else{ //Turn Down the brightness. What else is there to do? | |
for(int i = lastBrightness; i >= brightness; i--){ | |
analogWrite(color, i); | |
delay(DelayTime); | |
} | |
} | |
} |
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
#!/usr/bin/env python | |
import psutil | |
import serial | |
import time | |
# Variables | |
baud = 9600 | |
port = '/dev/ttyACM0' | |
ser = serial.Serial(port, baud) | |
try: | |
while 1: | |
cpuUsage = psutil.cpu_percent(interval = 1) # Sample CPU usage for ~1 second. | |
data = ord('C').to_bytes(1, byteorder='big') # For some reason I can't call #to_bytes on characters directly. | |
#print(data) # Debugging | |
ser.write(data) | |
data = int((cpuUsage / 100) * 255).to_bytes(1, byteorder='big') | |
ser.write(data) # Write the data. The conversion steps above are because I do everything in bytes on the | |
# Arduino side. They're not strictly necessary, as the Arduino has character classes. | |
time.sleep(0.1) | |
except KeyboardInterrupt: | |
print("\nCtrl-C detected. Stopping!") | |
finally: | |
# Do some cleanup... | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment