voici une brève explication sur la communication entre le module Arduino & PureBasic.
L'Arduino utilise le rs232 pour communiquer entre lui & son IDE de programmation ( même si il est branché en usb... )
L'arduino possède une librairie interne qui gère la communication serial , comme purebasic , à partir de là, on peut faire communiqué les 2 ( vive les standard de com ! )
une petite vidéo de démo :
http://www.youtube.com/watch?v=DsyJhQhF ... Cmh3ETU%3D
dans ce petit exemple , je contrôle une led bgr via une interface purebasic et 3 trackbars
le principe est simple :
PureBasic envois dans un premier temps quelle couleur il veut modifier , rouge , vert ou bleu. ( 0 , 1 ou 2 )
il attend un petit delais ( 10ms voir moins ) puis il envois une valeur de 0 à 255 qui sera l'intensité du signal pwm en sortie de l'arduino.
le code PB:
Code : Tout sélectionner
Declare SendLedValue(LedID.i, value.c)
#ARDUINO_COM = "COM35" ; CHANGE MOI !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#LED_RED = 0
#LED_GREEN = 1
#LED_BLUE = 2
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Port$ = #ARDUINO_COM
CompilerElse
Port$ = "/dev/ttyS0"
CompilerEndIf
UseJPEGImageDecoder()
img = LoadImage(#PB_Any,"led.jpg")
wnd = OpenWindow(#PB_Any,0,0,640,255,"ARDUINO LED RGB")
ImageGadget(#PB_Any,10,10,1,1,ImageID(img),#PB_Image_Border )
TextGadget(#PB_Any,190 + 64,10 ,120,32,"RED",#PB_Text_Center)
TextGadget(#PB_Any,190 + 196,10,120,32,"GREEN",#PB_Text_Center)
TextGadget(#PB_Any,190 + 328,10,120,32,"BLUE",#PB_Text_Center)
RED_GADGET = TrackBarGadget(#PB_Any ,190+64+46 , 40,32,190,0,255,#PB_TrackBar_Vertical)
GREEN_GADGET = TrackBarGadget(#PB_Any ,190+196+46 , 40,32,190,0,255,#PB_TrackBar_Vertical)
BLUE_GADGET = TrackBarGadget(#PB_Any ,190+328+46 , 40,32,190,0,255,#PB_TrackBar_Vertical)
If OpenSerialPort(0, Port$, 9600, #PB_SerialPort_NoParity , 8, 1, #PB_SerialPort_XonXoffHandshake, 4, 4)
Repeat
event = WindowEvent()
If event = #PB_Event_Gadget
gadgetID.i = EventGadget()
If gadgetID = RED_GADGET
value = GetGadgetState(RED_GADGET)
SendLedValue(#LED_RED,value)
EndIf
If gadgetID = GREEN_GADGET
value = GetGadgetState(GREEN_GADGET)
SendLedValue(#LED_GREEN,value)
EndIf
If gadgetID = BLUE_GADGET
value = GetGadgetState(BLUE_GADGET)
SendLedValue(#LED_BLUE,value)
EndIf
EndIf
Until event = #PB_Event_CloseWindow
SendLedValue(#LED_RED,0)
SendLedValue(#LED_GREEN,0)
SendLedValue(#LED_BLUE,0)
End
Else
MessageRequester("Error", "Can't open the serial port: "+Port$)
EndIf
Procedure SendLedValue(LedID.i, value.c)
Protected LED_COMP.c = 0
Select LedID
Case #LED_RED
LED_COMP = 0
WriteSerialPortData(0, @LED_COMP, 1)
Case #LED_GREEN
LED_COMP = 1
WriteSerialPortData(0, @LED_COMP, 1)
Case #LED_BLUE
LED_COMP = 2
WriteSerialPortData(0, @LED_COMP, 1)
EndSelect
Delay(1) ; Delais important , sinon risque pour l'arduino de recevoir en même temps les 2 données...
myValue.c = value
WriteSerialPortData(0, @myValue , 1)
Delay(1)
EndProcedure
le code Arduino:
Code : Tout sélectionner
// ARDUINO UNO ET INTERFACAGE AVEC PUREBASIC
// COMMANDE D'UNE LED BGR VIA LE PORT SERIE
// PUREBASIC ENVOIS LA COMMANDE EN 2 FOIS
// LA COMPOSANTE DANS UNE PREMIER TEMPS : R G ou B
// PUIS LA VALEUR DE 0 A 255
int byteReceived = 0; // Nombre d'octet recu par PB
#define LED_RED 3
#define LED_GREEN 5
#define LED_BLUE 6
int CURRENT_LED_PIN = 0;
byte RED_VALUE = 0;
byte GREEN_VALUE = 0;
byte BLUE_VALUE = 0;
// 0 = Attente de la composante
// 1 = Attente de la valeur
int MESSAGE_MODE = 0;
void setup()
{
// On ouvre le port serie , les pin 0 & 1 sont inutilisable
// sur la carte arduino
Serial.begin(9600);
// On passe les pin de commande de la led en mode "sortie"
/*
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
*/
}
void loop()
{
TOP:
if (Serial.available() > 0)
{
byteReceived = Serial.read();
byte data = byte(byteReceived);
if ( MESSAGE_MODE == 0 ) // On recois une commande pour selectionné la composante
{
switch(data)
{
case 0:
CURRENT_LED_PIN = LED_RED;
break;
case 1:
CURRENT_LED_PIN = LED_GREEN;
break;
case 2:
CURRENT_LED_PIN = LED_BLUE;
break;
}
MESSAGE_MODE = 1;
goto TOP;
}
if ( MESSAGE_MODE == 1 )
{
byte value = byte(byteReceived);
switch(CURRENT_LED_PIN)
{
case (LED_RED):
RED_VALUE = value;
break;
case (LED_GREEN):
GREEN_VALUE = value;
break;
case (LED_BLUE):
BLUE_VALUE = value;
break;
}
MESSAGE_MODE = 0;
goto TOP;
}
}
// Mise à jour de la led
analogWrite(LED_RED,RED_VALUE);
analogWrite(LED_GREEN,GREEN_VALUE);
analogWrite(LED_BLUE,BLUE_VALUE);
}