DMS8013: Post-it on the fridge

kotojelfelesegek

In this post I am going to show one little work I did based on serial communication between Arduino and Processing. As I stated on a previous post, my intention with these series of work was to work on “interaction in the distance”. In this case the interaction is between Arduino and Processing.

The exercise is about changing images in Processing through a pushbutton in Arduino. As I said before,  I am aiming this assessment as a sort practice for my final project.  This final project is going to be two objects (devices) that  communicate to each other and they are meant to be used by people who are in long-distance relationship. So I worked in this exercise thinking that it could be a little screen or display  that one of the member of the couple could put on the fridge (this would be what you see in Processing’s window) and the other guy has a button (the Arduino).  Hence, the idea is to recreate a post-it message’s thread that people who live together usually do when they are not home at the same time.  I am talking about leaving a message to the person you are living with by a post-it on the fridge. As the guys who are in a long-distance relationship can’t leave post-it physically, this a way to do something similar while being in the distance.

Hence, in technical terms, this could be summed up as it follows: the Processing has all the post-it messages that just show up when someone else pressed the button on Arduino.

The first thing I did was to practice the serial communication between Processing and Arduino with a simpler exercise: controlling the LED Pin on Arduino by mousePressed on Processing.

YouTube Preview Image

Afterwards, I tried to used the code done by Fabian Winkler, but it didn’t work out, I couldn’t run the sketches. So what did I do? I was thinking “hum if Tom was here he would tell me to break down the whole exercise and start from the beginning. So the first thing to do it’s to check if Arduino is working well”:

1) Checking Arduino: I had to add a LED pin to check if the breadboard was communicating with the laptop. In this checking, I didn’t include the serial communication.

YouTube Preview Image

2)Then I added the Serial Communication

YouTube Preview Image

So once I made sure that Arduino was working perfectly. I checked the Processing’s sketch.

4) Checking processing: as I way to check if Processing’s sketch was OK was to try to do the same thing that Arduino would do but inside of the Processing’s sketch. What do I mean? Instead of using Arduino to change the images on Processing, I would do it with the mouse with no need of using Arduino. It worked

YouTube Preview Image

Then, once I confirmed that both, Arduino and Processing work well independently, I had to try to a serial communication  between them. It does not work though. In the video we can see that there is serial communication coming from Arduino. Whenever I press the button, the number 1 shows up. However, for a reason that I don’t know, it seems that Processing is not receiving the information.

 YouTube Preview Image

But it does, according to this, it is receiving the information from Arduino,  because  at the bottom we can see the number 2 twice, which were sent by Arduino.

Screen Shot 2016-05-11 at 10.29.14

So, I still don’t know what the problem can be. I am figuring out as we still have some 2 days left before the deadline. I guess that being sick and with allergies is not a big help 🙁

See below the code I’ve used from the source I have mentioned at the top of the post:

ARDUINO

int switchPin = 2;
int LEDPin = 13;
int buttonState = 0;
// the setup routine runs once when you press reset:
void setup() {

// make the pushbutton’s pin an input:
pinMode(switchPin, INPUT);
pinMode(LEDPin,OUTPUT);

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {

// if switch is ON (pressed)
if (digitalRead(switchPin) == HIGH) {
Serial.write(“2”);

digitalWrite(LEDPin, LOW);

}else { //IF switch is OFF (not pressed)
Serial.write(“1”);
digitalWrite(LEDPin,HIGH);

}
delay(100);

}

IMG_20160510_150107057

PROCESSING

import processing.serial.*;

Serial port;
int val = 0;
int oldval = 0;
int numFrames = 4; // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];
void setup() {
size(500, 500);
frameRate(10);
images[0] = loadImage(“0P.jpg”);
images[1] = loadImage(“1P.jpg”);
images[2] = loadImage(“2P.jpg”);
images[3] = loadImage(“3P.jpg”);

 

port = new Serial(this, “/dev/cu.usbmodem1421”, 9600);
}

void draw(){

if (0 < port.available()) {
val = port.read();
}
if (val != oldval && val == 1) {
// the line above makes sure we advance only one frame with
// each pressing of the button
if (frame<numFrames-1) {
frame = (frame+1);
} else {
frame = 0;
}
}
image(images[frame], 0, 0);
oldval = val;
}

****BREAKING NEWS****

I’ve found where the problem was and now it works.

See first the final result:

YouTube Preview Image

If you want to read the messages see below :p

YouTube Preview Image

The problem was in Arduino, especially in  this part Serial.write(“1”); 

As the number was in between quotation marks, I was sending data from Arduino as text, instead of as numbers, which is the kind of information that Processing was expecting to receive.  Hence, it was not the same kind of information and it never arrive to “if”

See below the corrected code:

int switchPin = 2;
int LEDPin = 13;
int buttonState = 0;
// the setup routine runs once when you press reset:
void setup() {

// make the pushbutton’s pin an input:
pinMode(switchPin, INPUT);
pinMode(LEDPin,OUTPUT);

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {

// if switch is ON (pressed)
if (digitalRead(switchPin) == HIGH) {
Serial.write(2);

digitalWrite(LEDPin, LOW);

}else { //IF switch is OFF (not pressed)
Serial.write(1);
digitalWrite(LEDPin,HIGH);

}
delay(100);

}