Home > Articles > Hardware > Upgrading & Repairing

  • Print
  • + Share This
This chapter is from the book

Wireless LED Code

Upload the following code to both Arduinos. Remember, both modules are identical, down to the software. If you can’t remember how to upload sketches to your Arduino, Chapter 5, “Programming Arduino,” explains how.

#include <Wire.h>

const int buttonPin = 8;
const int ledPin = 13;
int buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT_PULLUP);
}

void process_incoming_command(char cmd)
{
  int speed = 0;
  switch (cmd)
  {
  case '1':
  case 1:
    digitalWrite(ledPin, LOW);
    break;
  case '0':
  case 0:
    digitalWrite(ledPin, HIGH);
    break;

  }
}


void loop() {
  if (Serial.available() >= 2)
  {
    char start = Serial.read();
    if (start != '*')
    {
     return;
    }

    char cmd = Serial.read();
    process_incoming_command(cmd);
  }

  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Serial.write('*');
    Serial.write(1);
  }
  else {
    Serial.write('*');
    Serial.write(0);
  }
  delay(50); //limit how fast we update
}
  • + Share This
  • 🔖 Save To Your Account