Skip to content

helloYouSketch.ino, annotated

Nik Martelaro edited this page Oct 7, 2018 · 5 revisions

<-- back to Understanding Hello You

helloYouSketch.ino

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

  1. Unix command review

  2. Hello You Introduction

  3. Log on to your IxE

  4. IxE overview

  5. Run the Hello You example

  6. Understanding Hello You

  7. Logging Out & Shutting Down

Post Novice
0. Logging in as Pi

  1. Preview the larger IxE image
  2. [Project Ideas](Project Ideas)

Also [Related Links](Related Links)

Clone this wiki locally