Code
int led = 13;
int comin=0;
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600); //This initialices the USB as a serial port
}
// the loop routine runs over and over again forever:
void loop() {
if(Serial.available()>0){
char incomingByte = (char)Serial.read();
if(incomingByte=='1'){
digitalWrite(led,HIGH);
delay(5000);
digitalWrite(led,LOW);
}
}
sensorValue = analogRead(sensorPin); //Reads the voltage of the resistor.
Serial.print("10101;");
Serial.println(sensorValue); //Writes the voltage on the Serial port.
// digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(int(sensorValue/5)); // wait an amount of time set by the resistor
// digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
// delay(int(sensorValue/5)); // wait again
if(comin=='1'){
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
}
}
Alles anzeigen
Und folgendes als Python Programm auf dem Raspberry:
Python
# -*- coding: utf-8 -*-
from serial import Serial
ser = Serial('/dev/ttyUSB0', 9600, 7, 'E', 1)
x=ser.readline()
ser.write(b'1')
print(x)
def cleanup( str ):
result = ""
for c in str:
if( (c >= "0") and (c <= "9") ):
result += c
return result
print( cleanup(x))
ser.write("1")
Alles anzeigen
Eigentlich sollte der Arduino durch den Python Befehl ser.write(“1″) eine LED 10 sekunden lang anschalten. Klappt aber leider nicht. Es werden nur die analogen Daten von Arduino zum Pi gesendet.