Day2 01_python_rgb
2024. 1. 18. 19:41ㆍarduino
- 파이썬과 통신해 led 제어
// 255,0,0RGB\n
struct RGBStruct {
int pins[3] = { 9, 10, 11 };
int data[3];
};
struct RGBStruct RGB;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(RGB.pins[i], OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
// 255,0,0RGB\n
if (Serial.available()>0){
for(int i=0; i<3; i++){
RGB.data[i] = Serial.parseInt();
analogWrite(RGB.pins[i], RGB.data[i]);
}
if (Serial.readStringUntil('\n').equals("RGB") == 1){
Serial.println("Received RGB Value");
}
}
}
- 파이썬 코드
import serial
import time
mySerial = serial.Serial("COM4", baudrate=9600, timeout=1.0)
time.sleep(2.0)
try:
while True:
sendData=input("DATA : ")
mySerial.write(sendData.encode())
time.sleep(1.0)
except KeyboardInterrupt:
print(" bye!!")
pass
mySerial.close()
'arduino' 카테고리의 다른 글
Day2 03_sensor_led (0) | 2024.01.18 |
---|---|
Day2 02_button (0) | 2024.01.18 |
Day1 08_rgb_SerialInput (0) | 2024.01.17 |
Day1 07_analogWrite (0) | 2024.01.17 |
Day1 06_LED8 (0) | 2024.01.17 |