Week 13: Networking and Communications
• Assignments
This week’s assignments
design and build a wired &/or wireless network connecting at least two processors
A wired SPI connection is created between the board from ‘input’ and ‘output’ weeks. The power is provided via output connector on ‘output’ board.
The idea is used the sensor from ‘input’ board and send the value to ‘output’ board via SPI.
Input Code
#include <SPI.h>
#define LED 3
#define IR A2
int adc=0;
void setup()
{
pinMode(LED, OUTPUT);
SPI.begin();
}
void loop()
{
digitalWrite(LED,HIGH);
adc=analogRead(IR);
delay(adc);
SPI.transfer(adc&0xFF);
digitalWrite(LED,LOW);
delay(256-adc);
}
Output Code
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#include <SPI.h>
#define PIN 4
#define NUMPIXELS 3
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 50; // delay for half a second
int result;
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
SPI.begin();
pixels.begin(); // This initializes the NeoPixel library.
}
void loop()
{
result=SPI.transfer(0x00);
set_LED(result);
delay(delayval);
}
void set_LED(int value)
{
if(value&0xC0)
{
pixels.setPixelColor(2, pixels.Color(20,0,0));
}
else
{
pixels.setPixelColor(2, pixels.Color(0,0,0));
}
if(value&0x30)
{
pixels.setPixelColor(1, pixels.Color(20,0,0));
}
else
{
pixels.setPixelColor(1, pixels.Color(0,0,0));
}
if(value&0x0F)
{
pixels.setPixelColor(0, pixels.Color(20,0,0));
}
else
{
pixels.setPixelColor(0, pixels.Color(0,0,0));
}
}
Get the files
You can download the source files directly.