-individual assignment: design and build a wired &/or wireless network with addresses connecting at least two processors
-group assignment: send a message between two projects
This week we have to create a wired or wireless network connecting at least two processors. First I followed a quick tutorial to understand what I2C communication is about, and played with some Arduinos.
I didn't completely manage to make the master talk to the two slaves independently. However I think I got a general idea of how I2C communication works...Anyway, considering the time I have this week I'll stop this I2C for now and move on to Serial Communication, which seems much easier to understand. This tutorial from SparkFun explains it pretty well.
A serial bus consists of just two wires: one for sending data and another for receiving. Serial devices should have two serial pins: the receiver, (RX) and the transmitter (TX).
I communicated the sonar board I did in week 10 to the speaker board I did in week 11. Considering the sonar board will only transmit data and the speaker board will only receive data, I connected them with just one cable, as seen below. Each board has it's own VCC and GND, even though I guess I could also make them share these...
These are the codes I wrote for the two boards. Basically I wanted the speaker to make a push everytime my hand gets close to the sonar. It was tricky to achieve one single push for every time I got my hand close to the sonar. After several tries, I solved it like this:
#include < SoftwareSerial.h >
SoftwareSerial mySonar(1, 2); // RX, TX
const int Trigger = 4;//Pin digital 4 para el Trigger del sensor
const int Echo = 3; //Pin digital 3 para el Echo del sensor
int i = 0;
void setup() {
pinMode(Trigger, OUTPUT); //pin como salida
pinMode(Echo, INPUT); //pin como entrada
digitalWrite(Trigger, LOW);//Inicializamos el pin con 0
mySonar.begin(9600);
mySonar.println("Hello, world?");
}
void loop()
{
long t; //timepo que demora en llegar el eco
long d; //distancia en centimetros
digitalWrite(Trigger, HIGH);
delayMicroseconds(10); //Enviamos un pulso de 10us
digitalWrite(Trigger, LOW);
t = pulseIn(Echo, HIGH); //obtenemos el ancho del pulso
d = t / 59; //escalamos el tiempo a una distancia en cm
if ((d < 10) && (i == 0)) {
mySonar.write('1');
mySonar.println();
mySonar.println("boom");
i = 1;
}
if ((d < 10) && (i >= 1)) {
mySonar.write('2');
mySonar.println();
mySonar.println("move away");
i = 2;
}
else {
mySonar.write('2');
mySonar.println();
i = 0 ;
}
}
#include < SoftwareSerial.h >
int speakerPin = 1;
SoftwareSerial myTotem(0, 2); // RX, TX
char val;
void setup() {
pinMode(speakerPin, OUTPUT);
myTotem.begin(9600);
myTotem.println("Hello, world?");
}
void loop() {
if (myTotem.available())
{
val = myTotem.read();
if (val == '1')
{
analogWrite(speakerPin, 255); // S
delay(20);
analogWrite(speakerPin, 0);
delay(20);
}
}
}
The code for the sonar board is basically the same as the one I used for week 10 (input devices), but adding the Serial Communication lines seen in week 12 (network & communication), and adding some statements. I'm sure these statements could be better written, but considering my no background in coding, I'm quite happy that I made it work.
Basically I defined a variable:
int i = 0;
Inside the loop(), if the distance read by the sonar is more than 10cm, the variable will remain the same, i=0. But if we get our hand closer to the sensor, as soon as the read distance gets less than 10cm, this variable will now be i=1 and the sonar board will send a '1' via serial, which will be read by the speaker board and make one speaker push.
if ((d < 10) && (i == 0)) {
mySonar.write('1');
mySonar.println();
mySonar.println("boom");
i = 1;
}
If the distance detected by the sonar is still below 10cm, on the next loops the variable will be now i=2 and the sonar board will now send a '2' via serial, which doesn't mean anything to the speaker board, so the speaker will not do anything.
if ((d < 10) && (i >= 1)) {
mySonar.write('2');
mySonar.println();
mySonar.println("move away");
i = 2;
}
In order to get another push, we need the variable to be i=0 again, which we will only achieve by making the distance read by the sonar higher than 10cm.
else {
mySonar.write('2');
mySonar.println();
i = 0 ;
That way when the variable is i=0 and the distance detected by the sonar gets lower than 10cm again, the sonar board will send another '1' via serial, which will be read by the speaker board and make another speaker push.
Finally, after loading the codes, here is the result! This is from my final project Totem. The speaker is inside the wooden volume containing smoke. The movement of the speaker is what pushes out the smoke rings. You can see more of this in my final project page