robpg:
Buonasera ho bisogno del vostro aiuto per modificare il mio codice che serve per azionare un motore. Sto compilando un programma con ARDUINO UNO. Sono riuscito a fare il mio programma e funziona tutto, l’unico problema che non riesco a risolvere è che una volta avviato lo sketch (per farlo utilizzo un telecomando) dove all’interno si trova la funzione delay l’arduino non accetta più nessun tipo di input da parte del telecomando. A me servirebbe poter interrompere o modificare il programma iniziato, tramite telecomando. Ho trovato delle guide in cui è possibile fare tutto sostituendo la funzione “delay” con la funzione “millis” ,purtroppo non riesco ad inserirla nel linguaggio.
Spero di aver esposto bene il mio problema…
Questo è il codice in questione:
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <IRremote.h> // usa libreria IR
int receiver = 7; // Ricevitore IR digital pin 7
IRrecv irrecv(receiver); // create instance of ‘irrecv’
decode_results results;
void setup()
{
pinMode(8, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(10,OUTPUT);//output
Serial.begin(9600); // for serial monitor output
irrecv.enableIRIn(); // Start the receiver
pinMode(13, OUTPUT); // Pin 13 output led
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" CIAO");
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(" SELEZIONA PROG");
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal
irrecv.resume();// receive the next value
}
{ while ( results.value == 0xFF30CF || results.value == 0xFF30CF ) //tasto 1 sul telecomando
{
digitalWrite(13, HIGH); // set the LED on
lcd.setCursor(0, 1); // (note: line 1 is the second row, since counting begins with 0):
lcd.print(" PROGRAMMA 001"); // print the number of seconds since reset:
//forward @ full speed
digitalWrite(8, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at full speed
delay(600000); //gira 10 minuti orario
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
delay(120000); //freno motore per 2 minuti
digitalWrite(8, LOW); //Establishes backward direction of Channel A
digitalWrite(9, HIGH); //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at half speed
delay(600000); //gira 10 minuti antiorario
digitalWrite(8, HIGH); //Eengage the Brake for Channel A
digitalWrite(13, LOW);
delay(3600000); //si ferma tutto per 1 ora
}
}
{ while ( results.value == 0xFF18E7 || results.value == 0xFF18E7 ) //tasto 2 sul telecomando
{
digitalWrite(13, HIGH); // set the LED on
lcd.setCursor(0, 1); // (note: line 1 is the second row, since counting begins with 0):
lcd.print(" PROGRAMMA 002"); // print the number of seconds since reset:
//forward @ full speed
digitalWrite(8, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at full speed
delay(300000); //gira 5 minuti orario
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
delay(60000); //freno motore per 1 minuti
digitalWrite(8, LOW); //Establishes backward direction of Channel A
digitalWrite(9, HIGH); //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at half speed
delay(300000); //gira 10 minuti antiorario
digitalWrite(8, HIGH); //Eengage the Brake for Channel A
digitalWrite(13, LOW);
delay(1800000); //si ferma tutto per 30 minuti
}
}
}
Cordiali saluti
se al primo post non si fanno le presentazioni ci saranno sempre problemi ]
prova questo e dimmi se risolvi non ho potuto testarlo perché non ho l’ardware
le righe che ho aggiunto (// elrospo)
alla fine dei due cicli millis viene riportato a zero,
se levi timer0_millis = 0, millis continua normalmente ma dopo un po di giorni potrebbe andare in overflow
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <IRremote.h> // usa libreria IR
int receiver = 7; // Ricevitore IR digital pin 7
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results;
extern unsigned long timer0_millis; // da elrospo
unsigned long timer_1_1 = 0 ; // da elrospo
unsigned long timer_1_2 = 0 ; // da elrospo
byte timer_signal_1_1 = 0 ; // da elrospo
byte timer_signal_1_2 = 0 ; // da elrospo
void setup()
{
pinMode(8, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(10,OUTPUT);//output
Serial.begin(9600); // for serial monitor output
irrecv.enableIRIn(); // Start the receiver
pinMode(13, OUTPUT); // Pin 13 output led
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" CIAO");
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(" SELEZIONA PROG");
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal
irrecv.resume();// receive the next value
}
{ while ( results.value == 0xFF30CF || results.value == 0xFF30CF ) //tasto 1 sul telecomando
{
digitalWrite(13, HIGH); // set the LED on
lcd.setCursor(0, 1); // (note: line 1 is the second row, since counting begins with 0):
lcd.print(" PROGRAMMA 001"); // print the number of seconds since reset:
//forward @ full speed
if (timer_signal_1_1 == 0) timer_1_1 = millis(), timer_signal_1_1 = 1 ; // da elrospo
if ((millis () - timer_1_1) < 600000)// da elrospo
digitalWrite(8, HIGH), //Establishes forward direction of Channel A
digitalWrite(9, LOW), //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at full speed
//delay(600000); //gira 10 minuti orario
if ((millis () - timer_1_1) > 600000 && (millis () - timer_1_1) < 720000 )// da elrospo
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
// delay(120000); //freno motore per 2 minuti
if ((millis () - timer_1_1) > 720000 && (millis () - timer_1_1) < 1320000 )// da elrospo
digitalWrite(8, LOW), //Establishes backward direction of Channel A
digitalWrite(9, HIGH), //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at half speed
//delay(600000); //gira 10 minuti antiorario
if ((millis () - timer_1_1) > 1320000 && (millis () - timer_1_1) < 4920000)// da elrospo
digitalWrite(8, HIGH), //Eengage the Brake for Channel A
digitalWrite(13, LOW);
if (millis () > 4920000 && timer_signal_1_1 == 1 ) timer0_millis = 0, timer_signal_1_1 = 0 ; //da el rospo
//delay(3600000); //si ferma tutto per 1 ora
}
}
{ while ( results.value == 0xFF18E7 || results.value == 0xFF18E7 ) //tasto 2 sul telecomando
{
if (timer_signal_1_2 == 0) timer_1_2 = millis(), timer_signal_1_2 = 1 ; // da elrospo
if ((millis () - timer_1_2) < 300000)// da elrospo
digitalWrite(13, HIGH), // set the LED on
lcd.setCursor(0, 1), // (note: line 1 is the second row, since counting begins with 0):
lcd.print(" PROGRAMMA 002"), // print the number of seconds since reset:
//forward @ full speed
digitalWrite(8, HIGH), //Establishes forward direction of Channel A
digitalWrite(9, LOW), //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at full speed
//delay(300000); //gira 5 minuti orario
if ((millis () - timer_1_2) > 300000 && (millis () - timer_1_2) < 360000 )// da elrospo
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
//delay(60000); //freno motore per 1 minuti
if ((millis () - timer_1_2) > 360000 && (millis () - timer_1_2) < 660000 )// da elrospo
digitalWrite(8, LOW), //Establishes backward direction of Channel A
digitalWrite(9, HIGH), //Disengage the Brake for Channel A
analogWrite(10, 255); //Spins the motor on Channel A at half speed
//delay(300000); //gira 10 minuti antiorario
if ((millis () - timer_1_2) > 660000 && (millis () - timer_1_2) < 2460000 )// da elrospo
digitalWrite(8, HIGH), //Eengage the Brake for Channel A
digitalWrite(13, LOW);
//delay(1800000); //si ferma tutto per 30 minuti
if (millis () > 2460000 && timer_signal_1_2 == 1 ) timer0_millis = 0, timer_signal_1_2 = 0 ; //da el rospo
}
}
}