-
Notifications
You must be signed in to change notification settings - Fork 8
helloYouSketch.ino, annotated
Wendy Ju edited this page Jan 30, 2016
·
5 revisions
(<- back to [Understanding-Hello-You]) ##helloYouSketch.ino
int led = 10; // led that we will toggle
char inChar; // character we will use for messages from the RPi
int button = 15;
int buttonState;
Variable declarations
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
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 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);
}
Serial input listener. This code listens for Serial messages from the RPi. 'H' characters turn the light on. Other characters turn the light off.
// 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");
}
}
}
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 buttoncode does not feature debouncing.
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)