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);
}

Clever ways of attaching components to your robot

During the development of Farrusco, we tried a lot of different ways to attach components to PVC supports, from hot-glue, to screws and double side-tape. Because this is intended to be sold as a kit the main keywords were: easy, cheap, last long as possible, and as a final thought “less is more”, what you will see in the fotos below was how André Almeida solved the issue, all credits goes to him. As you see we are using PVC but this can be done with cardboard, wood, acrylic, polymorph, you name it..

We will show bumper-sensors and a Sharp distance sensor, there won’t be much to say, photos speak for themselves:

What we will use is as simple as this:

Print

Starting with the bumpers, as you see they will fit perfectly inside the holes

02

Attach them with zip ties

03

let’s do the same with the Sharp IR

04

Fits perfectly on the pvc

05

use zipties to attach both to each other

06

fotos below speak for themselves:

06

07

08

07

Use your components shape as an advantage and try to be as simple as possible.

Now that you saw this, you can imagine other cool ways to attach other components and sensors to your robots!!

This Week In Hobby Robotics 2

LetMakeRobots.com

Imagine what happen when hobbyists, engeneers, artists and all kinds of cool people from very different backgrounds, gather with a single purpose in common, to create robots, share knowledge and information, and mostly have a lot of fun!

It’s a joy to see my Talkie Walkie being featured in this nice video. Fritsl and Rik, you guys rule!!

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

easing servo movements

I’ve been working on this for a while and finally I have the courage to post the results.

I’m also testing the pololu micro serial servo controller in the hope I could achieve better results. One good thing the pololu controller has, is the ability to set the speed of the movement, I guess it is a good plus to have this extra control. And using this feature together with the filter you get really cool and smooth movements, check the end of the video.

The point I don’t like in this experience is that the robot shakes a lot, but that is due to the weak support. I will have to arrange a solution to void so much shakiness.

The low pass filter below is what makes the desaceleration while the value reaches the destination point. I’ve posted the full code below the video.

filter: 0.05; // 1 = no filter // has to be between 0.01 and 1
destinationValue = 1000;
destinationValueFiltered = destinationValueFiltered * (1.0-filter) + destinationValue * filter;

—————————————————————————————————————————————————————-
—————————————————————————————————————————————————————-

Links section for further development:

Robert Penner’s Easing Equations:
http://robertpenner.com/easing/

Visualizers of easing equations:
http://www.robertpenner.com/easing/easing_demo.html
http://www.gizma.com/easing/

Processing Easing
http://processing.org/learning/basics/easing.html

Robert Penner’s easing equations demo in Processing:
http://jesusgollonet.com/processing/pennerEasing/

Animation & Shaping:
http://www.megamu.com/processing/shapetween/

——————————————————-

Arduino Forum Topics:

Converting Easing Functions ActionScript -> Wiring
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1187650321

—————————————————————————————————————————————————————-
—————————————————————————————————————————————————————-

Code commented in portuguese, please visit this topic: http://lusorobotica.com/index.php/board,38.0.html

Code to move one servo directly connected to the Arduino:

// easing servo movements with low pass filter
// the servo signal must be connected directly to the arduino pin 2 

// Time interval, this executes one piece of code from time to time
// Download Metro lybrary and instructions:
// http://www.arduino.cc/playground/Code/Metro
#include <Metro.h>
int duration = 3500; // duration of the interval in miliseconds
Metro intervaller = Metro (duration);

// MegaServo library
// download and instructions: 
// http://www.arduino.cc/playground/Code/MegaServo
#include
MegaServo servos;

// servos minimum and maximum position
#define MIN_POS 800 // the minuimum pulse width for your servos
#define MAX_POS 2200 // maximum pulse width for your servos

// servo pin
#define s0_pin 2

// variables to hold new destination positions
int d0; // destination0
int d0_sh; // destination0 to be smoothed

// the filter to be aplied
// this value will be multiplied by "d0" and added to "d0_sh"
float filtro = 0.05; // 0.01 to 1.0

// setup runs once, when the sketch starts
void setup() {
  // initialize serial comunication
  Serial.begin(9600);

  // set servo pin
  servos.attach(s0_pin);
}

// main program loop, executes forever after setup()
void loop() {

  // check the time interval, if it reaches limit "duration" goes back to one 
  // and runs the code
  if (intervaller.check() == 1) {

    // calculate a new random position between max and min values
    d0 = random(MIN_POS, MAX_POS); 

    // resets interval with a new random duration
    intervaller.interval(random(500,2000));
   }

  // smooth the destination value
  d0_sh = d0_sh * (1.0-filtro) + d0 * filtro;

  // assign new position to the servo
  servos.write(d0_sh);

  // delay to make the servo move
  delay(25);
}

And this is the code to use with the pololu micro serial servo controller:

// easing servo movements with a low pass filter and Pololu Micro Serial Servo Controller

// Time interval, this executes one piece of code from time to time
// Download Metro lybrary and instructions:
// http://www.arduino.cc/playground/Code/Metro
#include <Metro.h>
int duration = 3500; // duration of the interval in miliseconds
Metro intervaller = Metro (duration);

// servos minimum and maximum position
#define MIN_POS 500 // the minuimum pulse width for your servos
#define MAX_POS 5500 // maximum pulse width for your servos

// variables to hold new destination positions
int d0; // destination0
int d0_sh; // destination0 to be smoothed

// the filter to be aplied
// this value will be multiplied by "d0" and added to "d0_sh"
float filtro = 0.05;   // 0.01 to 1.0

// set servo speed, goes from 1 to 127
int servoSpeed = 120;

// setup runs once, when the sketch starts
void setup() {
  // initialize serial comunication
  Serial.begin(9600);

  // set servo pin and speed
  servoSetSpeed(0, servoSpeed);
}

// main program loop, executes forever after setup()
void loop() {

  // check the time interval, if it reaches limit "duration" goes back to one 
  // and runs the code
  if (intervaller.check() == 1) {

    // calculate a new random position between max and min values
    d0 = random(MIN_POS, MAX_POS);

    // resets interval with a new random duration
    intervaller.interval(random(500,3000));
  }

  // smooth the destination value
  d0_sh = d0_sh * (1.0-filtro) + d0 * filtro;

  // assign new position to the servo
  put(0,d0_sh);

  // delay to make the servo move
  delay(25);
}

// functions from this forum topic:
// http://forum.pololu.com/viewtopic.php?f=16&t=745&start=60&st=0&sk=t&sd=a

void put(int servo, int angle)
{
  //servo is the servo number (typically 0-7)
  //angle is the absolute position from 500 to 5500

  unsigned char buff[6];

  unsigned int temp;
  unsigned char pos_hi,pos_low;

  //Convert the angle data into two 7-bit bytes
  temp=angle&0x1f80;
  pos_hi=temp>>7;
  pos_low=angle & 0x7f;

  //Construct a Pololu Protocol command sentence
  buff[0]=0x80; //start byte
  buff[1]=0x01; //device id
  buff[2]=0x04; //command number
  buff[3]=servo; //servo number
  buff[4]=pos_hi; //data1
  buff[5]=pos_low; //data2

  //Send the command to the servo controller
  for(int i=0;i<6;i++){
    Serial.print(buff[i],BYTE);
  }

}

void servoSetSpeed(int servo, int speed){
  //servo is the servo number (typically 0-7)
  //speed is servo speed (1=fastest, 127=slowest)
  //set speed to zero to turn off speed limiting

  unsigned char buff[5];
  unsigned char speedcmd;

  speedcmd=speed&0x7f;//take only lower 7 bits of speed

  buff[0]=0x80;//start byte
  buff[1]=0x01;//device id
  buff[2]=0x01;//command number
  buff[3]=servo;//servo number
  buff[4]=speed;//data1

  for(int i=0;i<5;i++){
    Serial.print(buff[i],BYTE);
  }
}

Servo Motor Hack @ AltLab

Here’s another way to hack a servo motor. This time Njay shows us how to do it, and it is quite easy. The code to use is the same for a normal servo, when you point it to the center (90 degrees) it will stop, than if you increment the angle it will start moving on way, if you decrement the angle it will move the other way.

(the video is in portuguese)