Rieccomi.. Volevo farvi vedere il mio primo skatch funzionante, grazie ai vostri consigli... ![]()
byte pwm_1 = 3; //Pin Velocità motore 1
byte pwm_2 = 11; //Pin Velocità motore 2
byte pwm_3 = 5; //Pin Velocità motore 3
byte pwm_4 = 6; //Pin Velocità motore 3
byte pwm_5 = 10; //Pin Velocità motore 4
byte dir_1 = 12; //Pin Direzione motore 1
byte dir_2 = 13; //Pin Direzione motore 2
byte dir_3 = 8; //Pin Direzione motore 3
byte dir_4 = 9; //Pin Direzione motore 4
byte dir_5a = 2; //Pin Direzione moore 5
byte dir_5b = 7; //Pin Direzione motore 5
byte stato_1 = 0; //Stato direzione morore 1
byte stato_2 = 0; //Stato direzione morore 2
byte stato_3 = 0; //Stato direzione morore 3
byte stato_4 = 0; //Stato direzione morore 4
byte stato_5 = 0; //Stato direzione morore 5
byte command = 0; //Comando per movimento
byte pwm_speed = 255; // Velocità motori
unsigned long time_pause = 0; //parametro per pausa
boolean esci = false; //uscita da ciclo while
void setup()
{
// Inizio comunicazione seriale a 9600
Serial.begin(9600);
pinMode(pwm_1, OUTPUT);
pinMode(pwm_2, OUTPUT);
pinMode(pwm_3, OUTPUT);
pinMode(pwm_4, OUTPUT);
pinMode(dir_1, OUTPUT);
pinMode(dir_2, OUTPUT);
pinMode(dir_3, OUTPUT);
pinMode(dir_4, OUTPUT);
}
void loop()
{
if (Serial.available() > 0) { //Se sono presenti dati nel buffer
leggi_seriale(); //li leggo con la funzione
}
switch (command) { //Switch sul comando
case 'q':
if (stato_1 == 0)
{
pausa();
analogWrite(pwm_1, pwm_speed);
digitalWrite(dir_1, LOW);
Serial.println("Chiude pinza");
stato_1 = 1;
command = 0;
break;
}
else if (stato_1 == 1)
{
pausa();
analogWrite(pwm_1, pwm_speed);
digitalWrite(dir_1, HIGH);
Serial.println("Apre pinza");
stato_1 = 0;
command = 0;
break;
}
case 'w':
if (stato_2 == 0)
{
analogWrite(pwm_2, pwm_speed);
digitalWrite(dir_2, LOW);
Serial.println("Scende braccio 1");
stato_2 = 1;
command = 0;
pausa();
break;
}
else if (stato_2 == 1)
{
analogWrite(pwm_2, pwm_speed);
digitalWrite(dir_2, HIGH);
Serial.println("Sale braccio 1");
stato_2 = 0;
command = 0;
pausa();
break;
}
case 'e':
if (stato_3 == 0)
{
analogWrite(pwm_3, pwm_speed);
digitalWrite(dir_3, LOW);
Serial.println("Sale braccio 2");
stato_3 = 1;
command = 0;
pausa();
break;
}
else if (stato_3 == 1)
{
analogWrite(pwm_3, pwm_speed);
digitalWrite(dir_3, HIGH);
Serial.println("Scende braccio 2");
stato_3 = 0;
command = 0;
pausa();
break;
}
case 'r':
if (stato_4 == 0)
{
analogWrite(pwm_4, pwm_speed);
digitalWrite(dir_4, LOW);
Serial.println("Scende braccio 3");
stato_4 = 1;
command = 0;
pausa();
break;
}
else if (stato_4 == 1)
{
analogWrite(pwm_4, pwm_speed);
digitalWrite(dir_4, HIGH);
Serial.println("Sale braccio 3");
stato_4 = 0;
command = 0;
pausa();
break;
}
case 't':
if (stato_5 == 0)
{
analogWrite(pwm_5, pwm_speed);
digitalWrite(dir_5a, LOW);
digitalWrite(dir_5b, HIGH);
Serial.println("Gira a destra");
stato_5 = 1;
command = 0;
pausa();
break;
}
else if (stato_5 == 1)
{
analogWrite(pwm_5, pwm_speed);
digitalWrite(dir_5a, HIGH);
digitalWrite(dir_5b, LOW);
Serial.println("Gira a sinistra");
stato_5 = 0;
command = 0;
pausa();
break;
}
case '1':
pausa();
analogWrite(pwm_1, 0);
analogWrite(pwm_2, 0);
analogWrite(pwm_3, 0);
analogWrite(pwm_4, 0);
analogWrite(pwm_5, 0);
Serial.println("Stop!");
command = 0;
break;
case 's':
Serial.print ("Pwm impostato a: ");
Serial.println (pwm_speed, DEC);
command = 0;
break;
case 'a':
delay(250);
command =0;
break;
case 'p':
String command_string = String(5);
command_string = "";
byte cont = 0;
char char_in[3];
Serial.println("Modalità di programmazione..");
Serial.println("Inserire valore pwm");
command = 0;
esci = false;
Serial.flush();
while (esci == false) {
if (Serial.available()>0) {
while (Serial.available()>0) {
char_in[cont] = Serial.read();
cont ++;
delay(5);
}
for (int i =0; i <= cont-1; i++) {
command_string =command_string + char_in[i];
}
char buf[4];
command_string.toCharArray(buf, 4);
//if (command != 0) {
pwm_speed = atoi(buf);
Serial.print ("Velocità impostata a ");
Serial.println (pwm_speed, DEC);
command_string = "";
cont = 0;
esci = true;
}
}
}
}
void leggi_seriale(){
String command_string = String(10);
command_string = "";
byte cont = 0;
char char_in[10];
if (Serial.available()>0) {
command = Serial.read();
delay(2);
while (Serial.available()>0) {
char_in[cont] = Serial.read();
if (char_in[cont]== ';'){
break;
}
cont ++;
delay(2);
}
for (int i =0; i <= cont; i++) {
command_string =command_string + char_in[i];
}
char buf[cont+1];
command_string.toCharArray(buf, cont+1);
time_pause = atoi(buf);
}
command_string = "";
cont = 0;
}
void pausa(){
delay(time_pause);
time_pause=0;
}
In sostanza comanda un braccio robotizzato (http://www.thinkgeek.com/geektoys/science/b696/) in base ai comandi ricevuti tramite seriale. La sintassi prevista è:
qyyy;wyyy;eyyy; ecc...
Dove x, s e g sono i comandi per indicare quale motore attivare; mentre yyy è la variabile per la pausa. Non è obbligatoria inserirla ma può andare da 1 al valore massimo assegnabile alla funzione delay!
Lista comandi;
q = Apre/Chiude pinza
w = Alza/Abbassa braccio 1
e = Alza/Abbassa braccio 2
r = Alza/Abbassa braccio 3
t = Gira a destra/sinistra il braccio
1 = Arresta tutto
s = Restituisce il valore impostato ai PWM
a = Pausa predefinita di 250ms
p = Imposta velocità pwm
; = Separa un comando dall'altro
Purtroppo il braccio è azionato da motori a spazzole tipo mini 4wd (http://www.google.it/imgres?imgurl=http://superdroidrobots.com/pictures/Tamiya/FA-130.jpg&imgrefurl=http://www.baronerosso.it/forum/navimodellismo-motore/167363-vorrei-cosruire-2.html&usg=__zr4rLYD0HiXnlSBF22k9mHZPrt0=&h=300&w=450&sz=15&hl=it&start=0&zoom=1&tbnid=f0TqjJXwm5OaqM:&tbnh=127&tbnw=183&ei=ag6aTbeME4TIsgbWlazCCA&prev=/images%3Fq%3Dmini%2B4wd%26um%3D1%26hl%3Dit%26sa%3DN%26biw%3D1920%26bih%3D868%26tbm%3Disch0%2C114&um=1&itbs=1&iact=hc&vpx=1604&vpy=575&dur=751&hovh=183&hovw=275&tx=136&ty=86&oei=ag6aTbeME4TIsgbWlazCCA&page=1&ndsp=46&ved=1t:429,r:45,s:0&biw=1920&bih=868) ed è quindi impossibile essere a conoscenza della posizione! All'inizio, alla fine e durante gli spostamenti. Per questo ho usati i tempi per avere una gestione almeno approssimativa della cosa! Cosa che tra l'altro fà anche la scheda usb originale del robot.. (futuraelettronica.net PC controlled robotic arm - YouTube)
Spero non manchi niente, appena riesco metto qualche foto/video! Ciao..