
LEZIONE n° 7 – MOTORI / PROGRAMMAZIONE
La lezione è sui MOTORI IN CORRENTE CONTINUA, la loro gestione e sulle principali regole di PROGRAMMAZIONE.
Nell’esempio n° 11 si
Per provare a fare le modifiche proposte copiare il listato (colorato in rosso) ed incollarlo in un editor di solo testo [Notepad ad esempio]
listato programma esempio numero 11:
/*
/*
CONTOLLARE DUE MOTORI: 1° POTENZIOMETRO = VERSO // 2° POTENZIOMETRO VELOCITA’
*/
CONTOLLARE DUE MOTORI: 1° POTENZIOMETRO = VERSO // 2° POTENZIOMETRO VELOCITA’
*/
int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorValueSpeed = 0; // variable to store the value coming from the sensor
int sensorValueVersus = 0; // variable to store the value coming from the sensor
int sensorPin1 = A1;
int sensorValueSpeed = 0; // variable to store the value coming from the sensor
int sensorValueVersus = 0; // variable to store the value coming from the sensor
int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 10
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
int speed_outA = 100; //speed
int speed_outB = 100; //speed
int speedL = 60; //speed sotto i 40 non riescono a girare
int speedH = 120; //speed
int speed_outB = 100; //speed
int speedL = 60; //speed sotto i 40 non riescono a girare
int speedH = 120; //speed
int versusH = HIGH; //speed
int versusL = LOW; //speed
int versusL = LOW; //speed
int versusA = 0; //speed
int versusB = 0; //speed
int versusB = 0; //speed
int soglia = 500; //soglia
int TEMPO = 2500; //soglia
int TEMPO = 2500; //soglia
void setup()
{
Serial.begin(9600);
{
Serial.begin(9600);
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, speedL); //set both motors to run at (100/255 = 39)% duty cycle (slow)
analogWrite(pwm_b, speedL);
}
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, speedL); //set both motors to run at (100/255 = 39)% duty cycle (slow)
analogWrite(pwm_b, speedL);
}
void loop()
{
sensorValueSpeed = analogRead(sensorPin0); // read the state of the pushbutton value:
Serial.print(” sensor VELOCITA’ = ” ); // print the results to the serial monitor:
Serial.print(sensorValueSpeed);
Serial.print(“\n “);
delay(10);
sensorValueVersus = analogRead(sensorPin1); // read the state of the pushbutton value:
Serial.print(” sensor VERSO = ” ); // print the results to the serial monitor:
Serial.print(sensorValueVersus);
Serial.print(“\n “);
delay(10);
if (sensorValueVersus <= soglia)
{
versusA = versusL;
versusB = versusH;
Serial.print(” VERSO AVANTI \n ” );
delay(10);
}
else
{
versusA = versusH;
versusB = versusL;
Serial.print(” VERSO DIETRO \n ” );
delay(10);
}
{
sensorValueSpeed = analogRead(sensorPin0); // read the state of the pushbutton value:
Serial.print(” sensor VELOCITA’ = ” ); // print the results to the serial monitor:
Serial.print(sensorValueSpeed);
Serial.print(“\n “);
delay(10);
sensorValueVersus = analogRead(sensorPin1); // read the state of the pushbutton value:
Serial.print(” sensor VERSO = ” ); // print the results to the serial monitor:
Serial.print(sensorValueVersus);
Serial.print(“\n “);
delay(10);
if (sensorValueVersus <= soglia)
{
versusA = versusL;
versusB = versusH;
Serial.print(” VERSO AVANTI \n ” );
delay(10);
}
else
{
versusA = versusH;
versusB = versusL;
Serial.print(” VERSO DIETRO \n ” );
delay(10);
}
digitalWrite(dir_a, versusA); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, versusB); //Set motor direction, 3 high, 4 low
digitalWrite(dir_b, versusB); //Set motor direction, 3 high, 4 low
if (sensorValueSpeed <= soglia)
{
speed_outA = speedL;
speed_outB = speedL;
{
speed_outA = speedL;
speed_outB = speedL;
Serial.print(” VELOCITA’ BASSA \n ” );
delay(10);
}
else
{
speed_outA = speedH;
speed_outB = speedH;
delay(10);
}
else
{
speed_outA = speedH;
speed_outB = speedH;
Serial.print(” VELOCITA’ ALTA \n ” );
delay(10);
}
delay(10);
}
analogWrite(pwm_a, speed_outA); //set both motors to run at 100% duty cycle (fast)
analogWrite(pwm_b, speed_outB);
analogWrite(pwm_b, speed_outB);
Serial.print(“velocita motore A = ” );
Serial.print(speed_outA);
delay(10);
Serial.print(” velocita motore B = ” );
Serial.print(speed_outB);
Serial.print(“\n “);
Serial.print(“\n “);
delay(10);
Serial.print(speed_outA);
delay(10);
Serial.print(” velocita motore B = ” );
Serial.print(speed_outB);
Serial.print(“\n “);
Serial.print(“\n “);
delay(10);
delay(TEMPO);
}
}
*/
LE MODIFICHE PROPOSTE SONO:
CAMBIARE I TEMPI
CAMBIARE IL POTENZIOMETRO
ACCENDERE UN LED
Nell’esempio n° 12 il .
listato programma esempio numero 12:
/*
*/
LE MODIFICHE PROPOSTE SONO:
CONTARE I CICLI ESEGUITI E STAMPARE IL VALORE
OGNI 100 CICLI CAMBIARE IL VERSO DI ROTAZIONE
SE IL VALORE LETTO DEL SENSORE E’ PARI ACCENDERE UN LED
di seguito le slide della lezione
(42)