Ciao ragazzi con il vostro aiuto sono riuscito a convertire un sketch con la funzione millis.
Ora però c’è un problema che a me sembra molto strano: lo sketch all’interno dei voip loop è formato da due cicli che vengono cambiati con l’input di un telecomando. i due cicli sono identici (a parte la tempistica) il secondo ciclo (dove c’è il tasto 2 del telecomando) funziona alla perfezione, nel primo (dove c’è il tasto 1 del telecomando) invece la tempistica sembra non funziona proprio (praticamente il motore gira all’infinito e non rispetta i tempi del millis). mi chiedevo dove poteva essere l’errore?
vi allego lo sketch
// 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
}
Serial.println(millis ());
{
if ( results.value == 0xFF30CF || results.value == 0xFF30CF ) //tasto 1 sul telecomando
{
if (timer_signal_1_1 == 0) timer_1_1 = millis(), timer_signal_1_1 = 1 ; // da elrospo
if ((millis () - timer_1_1) < 6000)// 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 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
if ((millis () - timer_1_1) > 6000 && (millis () - timer_1_1) < 7200 )// da elrospo
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
// delay(120000); //freno motore per 2 minuti
if ((millis () - timer_1_1) > 7200 && (millis () - timer_1_1) < 13200 )// 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) > 13200 && (millis () - timer_1_1) < 49200)// da elrospo
digitalWrite(8, HIGH), //Eengage the Brake for Channel A
digitalWrite(13, LOW);
if (millis () > 49200 && timer_signal_1_1 == 1 ) timer0_millis = 0, timer_signal_1_1 = 0 ; //da el rospo
//delay(3600000); //si ferma tutto per 1 ora
}
}
{
if ( 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) < 3000)// 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) > 3000 && (millis () - timer_1_2) < 3600 )// da elrospo
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
//delay(60000); //freno motore per 1 minuti
if ((millis () - timer_1_2) > 3600 && (millis () - timer_1_2) < 6600 )// 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) > 6600 && (millis () - timer_1_2) < 24600 )// da elrospo
digitalWrite(8, HIGH), //Eengage the Brake for Channel A
digitalWrite(13, LOW);
//delay(1800000); //si ferma tutto per 30 minuti
if (millis () > 24600 && timer_signal_1_2 == 1 ) timer0_millis = 0, timer_signal_1_2 = 0 ; //da el rospo
}
}
}
grazie e cordiali saluti