New Basic Arduino Serial Communication

This is an updated example of basic serial communication between Arduinos, the old example can be found here.

Sender code:

[Arduino]

// SENDER
int b1 = 7;
int b2 = 8;

void setup()
{
Serial.begin(9600);
pinMode(b1, INPUT);
pinMode(b2, INPUT);
digitalWrite(b1, HIGH);
digitalWrite(b2, HIGH);
}

void loop()
{
byte val1 = (byte)digitalRead(b1);
byte val2 = (byte)digitalRead(b2);
Serial.write(‘a’); //SYNC char
Serial.write(val1);
Serial.write(‘b’); //SYNC char
Serial.write(val2);
delay(50);
}

 

[/Arduino]

Receiver Code:

// RECIEVER
byte led1;
byte led2;

void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
delay(1000);
}

void loop() {
if(Serial.available() > 0)
{
char inChar = Serial.read();
while(Serial.available()<=0);

if (inChar == ‘a’)
{
led1 = Serial.read();
}

if (inChar == ‘b’)
{
led2 = Serial.read();
}
}

digitalWrite(9, led1);
digitalWrite(8, led2);
delay(10);
}

Max + Arduino + Industrial Motor

At Artica, we had a request to link MAXMSP to an industrial motor for an artistic installation witch we will speak about at another time.

This motor is a true beast, and since we have never worked with such a thing we decided to ask for help to our electronics guru David Palma.

MAXMSP - ARDUINO - INDUSTRIAL MOTOR

MAXMSP - ARDUINO - INDUSTRIAL MOTOR

The motor controller:
MAXMSP - ARDUINO - INDUSTRIAL MOTOR

David developed an electronic circuit to simulate a PWM analog output from 0 to 10v (originally it gives 0 to 5v), and another circuit to switch motors direction, both circuits were assembled on a shield and connected to the motor controller.

The first circuit is a transducer:

TRANSDUCER

And this is the switch circuit that tell to the motor controller wich direction the motor will spin:

PNP

Then he builded an Arduino shield:

MAXMSP - ARDUINO - INDUSTRIAL MOTOR

And the final part was the Max patch that send the direction states and the PWM values to the Arduino:

MAXMSP - ARDUINO - INDUSTRIAL MOTOR

And this is the result:

MAXMSP – ARDUINO – INDUSTRIAL MOTOR from artica on Vimeo.

And last but not the least all the source codes can be downloaded here

RGB Mixer – Processing to Arduino

It has been a long time since I wanted to control the arduino with processing and I tried a lot of libraries, and a lot of processes and I always felt that none of those were suited for what I needed. I needed something simple to implement and easy to understand, and because I am not a programmer, I asked for help and @pauloricca replied to me with a quick, fast and really good solution.

In this example I have connected an RGB LED to the Arduino and on Processing we will have a simple mixer to fade in and out color channels. DON’T DO THIS, LED’s always need to have one resistor in series before. In this case I just wanted to show the serial communication part, and I skipped the resistor part, lazy me! Never do this, otherwise you will kill your leds very fast.

RGB Mixer - Processing to Arduino

On the Arduino side, I defined 3 digital output pins 9, 10, 11, these are PWM capable pins. Than I defined pin 8 as an OUTPUT, and digitallyWrite it to LOW, to be the GROUND pin. On the loop() function we used a switch() function that detects when the sync characters ’R’, ‘G’ and ‘B’ are received. These characters will tell us what value is coming next. The function GetFromSerial() is called everytime we need to read a value from the serial port.

void setup()
{
  // declare the serial comm at 9600 baud rate
  Serial.begin(9600);

  // output pins
  pinMode(9, OUTPUT); // red
  pinMode(10, OUTPUT); // green
  pinMode(11, OUTPUT); // blue

  // another output pin o be used as GROUND 
  pinMode(8, OUTPUT); // ground
  digitalWrite(8, LOW);
}

void loop()
{
  // call the returned value from GetFromSerial() function
  switch(GetFromSerial())
  {
  case 'R':
    analogWrite(9, GetFromSerial());
    break;
  case 'G':
    analogWrite(11, GetFromSerial());
    break;
  case 'B':
    analogWrite(10, GetFromSerial());
    break;

  }
}

// read the serial port
int GetFromSerial()
{
  while (Serial.available()<=0) {
  }
  return Serial.read();
}

On the Processing side, I am using a slider class adapted from anthonymatox.com, and I created 3 instances of this class (I assume you understand the concept of class). The important thing to understand here is the import of the Serial library, and the creation of a Serial object called “port”. On the setup() function I print out the available serial ports and than I defined which one is the Arduino port, on my case is the number 0, note that I am using mac, if you are using PC it should be COM1, COM2 or another COM#. Finally I am passing the values of the slider after I pass the sync character ‘R’, ‘G’, ‘B’.

RGB Mixer - Processing to Arduino

import processing.serial.*;
Serial port;

sliderV sV1, sV2, sV3;

color cor;

void setup() {
  size(500, 500);

  println("Available serial ports:");
  println(Serial.list());

  // check on the output monitor wich port is available on your machine
  port = new Serial(this, Serial.list()[0], 9600);

  // create 3 instances of the sliderV class
  sV1 = new sliderV(100, 100, 90, 255, #FF0000);
  sV2 = new sliderV(200, 100, 90, 255, #03FF00);
  sV3 = new sliderV(300, 100, 90, 255, #009BFF);
}

void draw() {
  background(0);

  sV1.render();
  sV2.render();
  sV3.render();

  // send sync character
  // send the desired value
  port.write('R');
  port.write(sV1.p);
  port.write('G');
  port.write(sV2.p);
  port.write('B');
  port.write(sV3.p);
}

/* 
Slider Class - www.guilhermemartins.net
based on www.anthonymattox.com slider class
*/
class sliderV {
  int x, y, w, h, p;
  color cor;
  boolean slide;

  sliderV (int _x, int _y, int _w, int _h, color _cor) {
    x = _x;
    y = _y;
    w = _w;
    h = _h;
    p = 90;
    cor = _cor;
    slide = true;
  }

  void render() {
    fill(cor);
    rect(x-1, y-4, w, h+10);
    noStroke();
    fill(0);
    rect(x, h-p+y-5, w-2, 13);
    fill(255);
    text(p, x+2, h-p+y+6);

    if (slide==true && mousePressed==true && mouseX<x+w && mouseX>x){
     if ((mouseY<=y+h+150) && (mouseY>=y-150)) {
        p = h-(mouseY-y);
        if (p<0) {
          p=0;
        }
        else if (p>h) {
          p=h;
        }
      }
    }
  }
}

RGB Mixer - Processing to Arduino

RGB Mixer - Processing to Arduino

RGB Mixer - Processing to Arduino

Serial Comunication between Arduinos – With Wire & Wireless

— please check an updated code sample on the following post —
http://lab.guilhermemartins.net/2013/08/01/new-basic-arduino-serial-communication/

 

I´ve created this example to make two Arduinos talk in a simple and clear fashion.
You will see below an example with wires, and another one without wires.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Setup with TX and RX pins connected by wire:

On the sender Arduino there is a potenciometer, the values are read and sent through the serial port.
On the receiver Arduino one Led intensity will change accordingly the pot value.

Tx on Arduino#1 -> connect to ->  Rx  on Arduino#2
Rx on Arduino#1 -> connect to ->  Tx on Arduino#2

NOTE:
Don´t forget to disconnect Tx / Rx wires before upload

// SENDER

int analogValue5, val5;

void setup() {
// Serial port enable
Serial.begin(19200);
}

void loop() {
// read analog pin 5
analogValue5 = analogRead(5);

// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);

// send the value to the serial port
Serial.println(val5, BYTE);

}

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

// RECIEVER

byte incomingByte;

void setup() {
// Serial port enable
Serial.begin(19200);

// declare pin 11 as output, this is the LED
pinMode (11, OUTPUT);
}

void loop() {

// if there is bytes available coming from the serial port
if (Serial.available()) {

// set the values to the ‘incomingByte’ variable
incomingByte = Serial.read();

// write the value to the pin 11
analogWrite(11, int(incomingByte));

}
}


– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Wireless setup with XBee modules:

Recently I got two XBee modules, and here is the setup to make them talk to each other:
As a start I used 2 potenciometers to control the intensity of two LEDs and It works pretty well.  :]

I´m using this adapters to connect the XBee to the circuit. They are very simple to assemble and to use.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

// SENDER

int analogValue2, analogValue5, val2, val5;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(2);
analogValue5 = analogRead(5);

// remap values

val2 = map(analogValue2, 0, 1023, 253, 0);  // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 253, 0);

Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);

Serial.print(255, BYTE); //SYNC char
Serial.print(val5, BYTE);

delay(150);
}

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

// RECIEVER

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println(“Ready!”);

// led pins

pinMode (5, OUTPUT);
pinMode (6, OUTPUT);

delay(1000);

}

void loop() {
if (Serial.available()) {   // are there any bytes available on the serial port ???

// assign bytes to the var ‘incomingByte’
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess   :)

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print(“Sensor 1 = “);
Serial.print(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print(“  Sensor 2 = “);
Serial.print(int(sensor2));
}
}

analogWrite (5, sensor1);
analogWrite (6, sensor2);
}