Pages

19 juil. 2018

Serial - Arduino, Processing

Simple liaison série entre Arduino Nano et Processing.

Ici on voit un cube mappé manipulé grâce aux potentiomètres attachés au arduino Nano.


Ce module en bois a été découpé au laser, et sert principalement a faire des tests rapides.

Dans le code exemple on peut voir l'utilisation de la lecture analogique et l'affichage des leds ..

Code Arduino (serial.ino) et processing (serial.pde) partiel:


/*ARDUINO*/
int b0, a1,a2,a3,a4,a5,a6;
int smooth_a1,smooth_a2,smooth_a3,smooth_a4,smooth_a5,smooth_a6;
int count[14];
void setup()
{
Serial.begin(9600); // Calculation of datas sent: 7 inputs x 24 bits (3 packages of 8bits) x 30hz (30 times / sec) = 5040 minimum; At 9600 baud, it is 57Hz
Serial.println("Welcome to Serial Lab");
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, INPUT);
pinMode(13, OUTPUT);
smooth_a1 = analogRead (A1);
smooth_a2 = analogRead (A2);
smooth_a3 = analogRead (A3);
smooth_a4 = analogRead (A4);
smooth_a5 = analogRead (A5);
smooth_a6 = analogRead (A6);
}
void loop()
{
//Serial.flush();
b0 = digitalRead(10);
a1 = analogRead (A1);
a2 = analogRead (A2);
a3 = analogRead (A3);
a4 = analogRead (A4);
a5 = analogRead (A5);
a6 = analogRead (A6);
smooth_a1 = smooth_data(a1, smooth_a1);
smooth_a2 = smooth_data(a2, smooth_a2);
smooth_a3 = smooth_data(a3, smooth_a3);
smooth_a4 = smooth_data(a4, smooth_a4);
smooth_a5 = smooth_data(a5, smooth_a5);
smooth_a6 = smooth_data(a6, smooth_a6);
Serial_send(0x80,b0);
Serial_send(0x81,smooth_a1);
Serial_send(0x82,smooth_a2);
Serial_send(0x83,smooth_a3);
Serial_send(0x84,smooth_a4);
Serial_send(0x85,smooth_a5);
Serial_send(0x86,smooth_a6);
Serial_receive();
}
/*
* Send an int (limited to 14 bits) by 3 bytes of 8 bits:
* 14 bits is a choice of precision, if more need, just add more bytes.
* 1st byte: witch button / potentiometer (message from 128 to 255: 0x80 to 0xff).
* 2nd byte: first part of data. It is int, split on 2 bytes. Each ones coded on 7bits.
* 3nd byte: second part of data.
*/
void Serial_send(byte message, int value)
{
Serial.write( message ); // Message. Send init byte. Data send here is inclusive from 128 to 255.
Serial.write((value >> 7) & 0x7f ); // Data1/2. Send first part. (127 on 7 bits). Shift int to store 7 first bits of 14 bits of int. Bitwise "and" (&) 0x7f, to filter 8bits an 7bits (B01111111)
Serial.write( value & 0x7f ); // Data2/2. Send second part.(127 on 7 bits). Just bitwise on 7 bits.
}
void Serial_receive()
{
int value = -1;
while (Serial.available() >= 2)
{
if (Serial.read() == 97) // if it is "a"
{
value = Serial.read();
}
}
switch (value)
{
case 48: Set_led(13); break; // case "0"
case 49: Set_led(4); break; // case "1"
case 50: Set_led(5); break; // case "2"
case 51: Set_led(6); break; // case "3"
case 52: Set_led(7); break; // case "4"
case 53: Set_led(8); break; // case "5"
case 54: Set_led(9); break; // case "6"
case 55: Anim_led(); break; // case "7"
}
}
int smooth_data(int data, int data_buffer)
{
return int(0.35 * data + 0.65 * data_buffer);
}
/*
* Toggle led, pin numbers are: 13, 4, 5, 6, 7, 8, 9
*/
void Set_led(byte pin)
{
count[pin] = !count[pin];
digitalWrite(pin,count[pin]);
}
view raw serial.ino hosted with ❤ by GitHub
/*PROCESSING (partial code)*/
// Determines the USB port used by index of list
short portIndex = 1;
import processing.serial.*;
Serial port; // Create object from Serial class
int b0 =-1, a1=-1, a2=-1, a3=-1, a4=-1, a5=-1, a6=-1;
void serial_init()
{
String com[] = Serial.list();
for (int id=0; id<com.length; id++)
println(com[id]);
// Open the port of board is connected and use speed 9600 bps
port = new Serial(this, com[portIndex], 9600);
}
void serial_read()
{
while (port.available() >= 3)
{
int msg = port.read();
if (msg >127)
{
int value = (port.read() << 7) | (port.read());
switch (msg)
{
case 128: b0 = value; break;
case 129: a1 = value; break;
case 130: a2 = value; break;
case 131: a3 = value; break;
case 132: a4 = value; break;
case 133: a5 = value; break;
case 134: a6 = value; break;
}
}
}
}
void serial_write()
{
if (frame%10 == 1) port.write("a0");
}
view raw serial.pde hosted with ❤ by GitHub

Aucun commentaire: