ragazzi, di nuovo salve a tutti....ho un problemino con un controller pololu ed arduino. non riesco a far partire i motori e a farli rimanere accesi. Qualcuno sa spiegarmi il perchè?
#include <SoftwareSerial.h>
#define txPin 7 // pin di trasmissione per il motor controller"
#define rxPin 6 // inutilizzato
#define LED_PIN 13
#define MOTOR_RESET_PIN 8
#define MOTOR_SPEED 0x64
unsigned char motorCommandBuffer[6];
SoftwareSerial motorSerial = SoftwareSerial(rxPin, txPin);
/**************** impostazione variabili ******************************/
int val;
int distance = 0;
int servoPin = 2; // control pin for servo motor
int Sinistra = 600; // minimum servo position
int Destra = 2500; // maximum servo position
int pulseWidth; // servo pulse width
int Lettura ; // raw user input
int i ;/*= 36;*/
int a;
int spazioDX;
int spazioSX;
int spazioAV;
boolean muoviti = true;
/********************** funzione richiamata dall'interrupt timer2 ************************************/
ISR(TIMER2_OVF_vect)
{
distanza();
if (distance<=35)
Serial.print("arresto imprevisto");
resetMotor();
motorBackward();
}
void setup()
{
pinMode(LED_PIN,OUTPUT);
pinMode(MOTOR_RESET_PIN,OUTPUT);
pinMode(txPin, OUTPUT); // Serial Transmit Pin
Serial.begin(9600);
motorSerial.begin(9600);
resetMotor();
}
void loop()
{
spazioAV = distanza();
// Serial.println(spazioAV);
if (spazioAV >= 35)
{
Serial.print(muoviti);
motorForward();
}
else
{
guardaaDX();
spazioDX = distanza();
Serial.println(spazioDX);
if (spazioDX >= 35)
{
centro();
resetMotor();
turnRight();
resetMotor();
}
else
{
guardaaSX();
spazioSX = distanza();
Serial.println(spazioSX);
if (spazioSX >= 35)
{
centro();
resetMotor();
turnLeft();
resetMotor();
}
else
{
resetMotor();
motorBackward();
resetMotor();
}
}
}
}
void motorForward()
{
//left motor
motorCommandBuffer[0]=0x80; //start byte - do not change
motorCommandBuffer[1]=0x00; //Device type byte ? do not change
motorCommandBuffer[2]=0x00; //Motor number and direction byte; motor one =00,01 motorCommandBuffer[3]=MOTOR_SPEED; //Motor speed "0 to 128" in hex (ex 100 is 64 in hex)
for(int i=0; i<4; i++)
{motorSerial.print(motorCommandBuffer[i], BYTE);}
//right motor
motorCommandBuffer[0]=0x80;
motorCommandBuffer[1]=0x00;
motorCommandBuffer[2]=0x02; //Motor number and direction byte; motor two=02,03
motorCommandBuffer[3]=MOTOR_SPEED;
for(int i=0; i<4; i++)
{motorSerial.print(motorCommandBuffer[i], BYTE);}
}
void motorBackward()
{
motorCommandBuffer[0]=0x80; //start byte - do not change
motorCommandBuffer[1]=0x00; //Device type byte ? do not change
motorCommandBuffer[2]=0x01; //Motor number and direction byte; motor one =00,01
motorCommandBuffer[3]=MOTOR_SPEED; //Motor speed "0 to 128" in hex (ex 100 is 64 in hex)
for(int i=0; i<4; i++)
{motorSerial.print(motorCommandBuffer[i], BYTE);}
//right motor
motorCommandBuffer[0]=0x80;
motorCommandBuffer[1]=0x00;
motorCommandBuffer[2]=0x03; //Motor number and direction byte; motor two=02,03
motorCommandBuffer[3]=MOTOR_SPEED;
for(int i=0; i<4; i++)
{motorSerial.print(motorCommandBuffer[i], BYTE);}
}
void resetMotor() //reset dei motori, attendere 150 ms tra un operazione disattivazione e attivazione
{
deactivateMotor();
delay(150);
Serial.print("arresto");
activateMotor();
delay(150);
}
void deactivateMotor() //disattiva i motori
{
digitalWrite(MOTOR_RESET_PIN,LOW);
digitalWrite(LED_PIN,LOW);
}
void activateMotor() //attiva i motori
{
digitalWrite(MOTOR_RESET_PIN,HIGH);
digitalWrite(LED_PIN,HIGH);
}
Sicuro che non sono esperto sia di programmazione che di elettronica, è per questo che mi trovo qui.
grazie per l'aiuto che vorrete darmi.