Sunday, March 24, 2013

Cubieboard + arduino + protocol

Light Sensor

This is another step in the communication between an arduino and a cubieboard.
By te moment I'm only using ttyS0 at the cubieboard, but for more complicated develops should be necessary to use the GPIO

The exercises consist in to detect a value and send it to the cubieboard, sound simple,itsn't ?

Well the circuit is quite simple, its an LDR (light dependent resistor ) connected to an analog connection at the arduino.

With the value of it we change the delay of blink, and with less light the led blink faster.


This is the basic circuit which is a modification from the project 14 (Light sensor from http://math.hws.edu/vaughn/cpsc/226/docs/askmanual.pdf)



Light sensor + serial connection
Well the circuit is just to see how to send the information from the arduino to our cubieboard, and how to interpret the data.

Now it's possible to receive the data and manipulate

There are a little bit of programming at the cubieboard to open the serial connection (ttyS0) ill try to summarize and it could be a bit tedious and

//I searched the code from google, there are a lot of information
//But is there any error or doubt don't hesitate to comment

[CODE FROM CUBIEBOARD]
//Open the serial port TTYS0 = /dev/ttyS0


    fd = open (TTYS0, O_RDWR | O_NOCTTY);
    if (fd < 0) { perror(TTYS0); return (1);}
    tcgetattr(fd,&oldtio);
    memset(&newtio,0, sizeof(struct termios));

//It was really necessary to change the parameters 
//to accommodate the serial from arduino to the serial of cubieboard
// More information here http://www.easysw.com/~mike/serial/serial.html

    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | IGNBRK | IMAXBEL ;
    newtio.c_cc[VTIME]=8;
    tcflush (fd,TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

//READ
    while (STOP == FALSE)
    {
        res =read (fd,buf,255);
//Eliminate the "intro" of the data
        buf[res-1]=0;
        printf("%s\n",buf);
        if (buf[0]=='z') STOP =TRUE;
    }
    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
===================================

For the arduino code I'm making a library, so it will be easy to improve, correct, and share.

[CODE FROM ARDUINO]
//LIBRARY
Protocol::Protocol(int pin)
{
    _pin=pin;
}


void Protocol::send(String msg)
{
    Serial.print(_id +":");
    Serial.println(msg);
}

void Protocol::setId(String id)
{
    _id = id;
}  
//CODE
 #include <protocol.h>

Protocol protocolLight(ledPin);

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  protocolLight.setId("light");

}

void loop()
{
  lightVal = 1023 - analogRead(ldrPin);
  protocolLight.send(String(lightVal));
  digitalWrite(ledPin,HIGH);



No comments:

Post a Comment