The purpose of this week was to make two boards communicate, so I started this off with wired communication. One board transmitted information on a button press to a different receiving board the would turn on the LED strip in response to the button press.
The code used for the transmitting board:
//Code to transmit
//Microcontroller has a button input and a digital output.
//The digital output can be connected to a digital input of a receiving microcontroller to make a simple network.
//The wired-together devices must share a common ground!
const int out_pin = 14;
const int button_pin = 9;
void setup() {
pinMode(button_pin, INPUT_PULLUP); //This is the button pin.
pinMode(out_pin, OUTPUT); // pin 5 for digital Write
Serial.begin(9600); //for the USB serial devices, this baud rate is meaningless - can be "0"
}
void loop() {
int buttonState = digitalRead(button_pin);
Serial.println(buttonState);
digitalWrite(out_pin, buttonState);
delay(10);
}
The code used for the receiving board:
//Code to turn on an LED in response to a change in input pin.
//PS70 use as a simple example of receiving for a wired network 3/30/20. Rob Hart.
#include
#define PIN 11
#define NUMPIXELS 10
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(7, INPUT_PULLUP); // pin 7 for digitial read
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(20); // Set BRIGHTNESS low to reduce draw (max = 255)
}
void loop() {
strip.clear(); // Set all pixel colors to 'off'
if (digitalRead(7)) {
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i < NUMPIXELS; i++) { // For each pixel...
// strip.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
strip.setPixelColor(i, strip.Color(100, 0, 70));
strip.show(); // Send the updated pixel colors to the hardware.
delay(200); // Pause before next pass through loop
}
}
else {
strip.clear();
}
}