-
Notifications
You must be signed in to change notification settings - Fork 8
helloYouSketch.ino, annotated
<-- back to Understanding Hello You
Variable declarations
int led = 10; // led that we will toggle
char inChar; // character we will use for messages from the RPi
int button = 15;
int buttonState;
Setup function. The invocation of serial is always necessary for the IxE Arduino because otherwise the Arduino is not communicating with the IxE RPi.
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
Serial input listener. This code listens for Serial messages from the RPi. 'H' characters turn the light on. Other characters turn the light off.
void loop() {
// read the character we recieve on the serial port from the RPi
if(Serial.available()) {
inChar = (char)Serial.read();
}
// if we get a 'H', turn the LED on, else turn it off
if(inChar == 'H'){
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
}
Button check. This code attends to changes on the voltage on the 'button' pin. If the voltage on that pin goes high, it sends a serial message 'light' to the RPi. If the voltage goes low, it sends the message 'dark'. Note that this button code does not feature [debouncing[(https://www.arduino.cc/en/Tutorial/Debounce).
// Button event checker - if pressed, send message to RPi
int newState = digitalRead(button);
if (buttonState != newState) {
buttonState = newState;
if(buttonState == HIGH){
Serial.println("light");
}
else{
Serial.println("dark");
}
}
}
<-- back to Understanding Hello You
IxE Workshop
For Novices
Post Novice
0. Logging in as Pi
- Preview the larger IxE image
- [Project Ideas](Project Ideas)
Also [Related Links](Related Links)