Part 6 - speaking to other nodes
Lets bring our buzzer back into play as shown in the circuit below.
The circuit:
We now want to receive a message from the sending unit and then pass it on to one of our neighbours, lets make our code a bit more interesting and make use of some of the bits we have already used. This time the sending unit will broadcast a message with a value between 0 and 1024 just like when we used a potentiometer to control things before.
Check the message is meant for your node and then use this 0-1024 value to:
- Control the LED colour with the colour mixing code we found at this resource. You might want to put it in a function of its own and call it from the main loop, remember to check the pins!
- Control the buzzer with the 'tone()' function by mapping the 0-1023 range to 220-4400 for frequency.
- Turn the LED and buzzer on for one second, then turn them both off
- Pass the input value on to the next node, look at the sender example in Part 5 if you are not sure how.
+ Hint 1
We have everything we need between the previous exercises:
- go back to the exercise 4 solution, it has the colour mixer and tone code in it and forms a great basis for this sketch
- extract the Serial reading and address/code matching stuff from the solution in exercise 5 and merge it into the part 4 sketch
- look at the sender example in part 5 to see how to send a message to another node
- you will need to set up your own address and the node you are going to send to!
+ The solution
/*
* Receives serial, breaks two ints apart
* and uses them as an address and code for LED
* and tone playing. Then sends code to next
* node.
*
* Luke Woodbury 25 Jan 2017
* dotLib.org
*/
//LED pins and variables to store values
const int redPin = 10;
const int grnPin = 6;
const int bluPin = 9;
int redVal = 0;
int grnVal = 0;
int bluVal = 0;
//buzzer pin and variable
const int buzzPin = 11;
int toneVal = 100;
int thisNode = 2;
int nextNode = 3;
void setup()
{
//set up serial for the xbee comms
Serial1.begin(9600);
//set up serial for printing to monitor etc
//Serial.begin(9600);
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
void loop(){
if (Serial1.available() >= 3) {
//use 'parseInt' to set the address/code variable
int address = Serial1.parseInt();
int code = Serial1.parseInt();
//Serial.print(address);
//Serial.print(" ");
//Serial.println(code);
//if address matches our node number
if(address == thisNode){
//use to animate LED and buzzer
LEDanimate(code);
playTone(code);
delay(1000);
allStop();
//Send to the next node
Serial1.print(nextNode);
//Serial.print(nextNode);
//send a space
Serial1.print(' ');
//Serial.print(' ');
//... then send the code
Serial1.print(code);
//Serial.println(code);
//...then another space to stop parseInt waiting
Serial1.print(' ');
}
}
}
void LEDanimate(int LEDval){
if (LEDval < 341) // Lowest third of the pot range (0-340)
{
LEDval = (LEDval * 3) / 4; // Normalize to 0-255
redVal = 256 - LEDval; // Red from full to off
grnVal = LEDval; // Green from off to full
bluVal = 1; // Blue off
}
else if (LEDval < 682) // Middle third of pot range (341-681)
{
LEDval = ( (LEDval-341) * 3) / 4; // Normalize to 0-255
redVal = 1; // Red off
grnVal = 256 - LEDval; // Green from full to off
bluVal = LEDval; // Blue from off to full
}
else // Upper third of potentiometer"s range (682-1023)
{
LEDval = ( (LEDval-683) * 3) / 4; // Normalize to 0-255
redVal = LEDval; // Red from off to full
grnVal = 1; // Green off
bluVal = 256 - LEDval; // Blue from full to off
}
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
}
void playTone(int toneVal){
toneVal = map(toneVal, 0, 1023, 220, 4400);
tone(buzzPin, toneVal);
}
void allStop(){
analogWrite(redPin,0);
analogWrite(grnPin,0);
analogWrite(bluPin,0);
noTone(buzzPin);
}
