Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
4 pulsanti temporizzati - 4 switch
#1
Wink 
Ciao a tutti, sperando di fare cosa gradita posto il mio primo firmware progettato per arduino cactus o lilypad, composto da 4 pulsanti temporizzati con uscite pin 9-8-7-6 (nel caso in questione 1 sec ma modificabile a piacimento) e 4 switch uscite pin 5-4-3-2.

[Image: 4pulsanti_4switch.jpg]
 
Code:
#define ESP_PROGRAM_PIN  12
#define ESP_ENABLE_PIN  13

enum SM_B1
{
    CMD_B1_WHITING=0,
    CMD_B1_EXEC=1,
    CMD_B1_END=2,
};

enum SM_B2
{
    CMD_B2_WHITING=0,
    CMD_B2_EXEC=1,
    CMD_B2_END=2,
};

enum SM_B3
{
    CMD_B3_WHITING=0,
    CMD_B3_EXEC=1,
    CMD_B3_END=2,
};

enum SM_B4
{
    CMD_B4_WHITING=0,
    CMD_B4_EXEC=1,
    CMD_B4_END=2,
};

unsigned char StateMachine_B1=CMD_B1_WHITING;
unsigned char StateMachine_B2=CMD_B2_WHITING;
unsigned char StateMachine_B3=CMD_B3_WHITING;
unsigned char StateMachine_B4=CMD_B4_WHITING;
unsigned int  Timer_B1=0;
unsigned int  Timer_B2=0;
unsigned int  Timer_B3=0;
unsigned int  Timer_B4=0;


void setup() 
{

  // Initialize Serial Port
  Serial1.begin(57600); //seriale wifi
  
  pinMode(ESP_ENABLE_PIN, OUTPUT);
  pinMode(ESP_PROGRAM_PIN, OUTPUT);


   
  // Set ESP module in RUN mode on reset 
  digitalWrite(ESP_PROGRAM_PIN, HIGH);  
  digitalWrite(ESP_ENABLE_PIN, HIGH);

 // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

//  OCR1A = 31250;            // compare match register 16MHz/256/2Hz
  OCR1A = 3125;            // compare match register 16MHz/256/2Hz
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts

  
  // Let uPanel start 
  delay(5000);

  // Discharge old partial messages
  Serial1.println("");

 // Enable real-time response
  Serial1.println("$PING 200");

  // Send The Panel (A LED and a Switch)
  Serial1.println("$P:D!g11;=T*20:HOME PANEL;=</{d5*15^B1%50,13r20!CCC:Pulsante 1;&&&&L1G:0;}</{d5*15^B2%50,13r20!CCC:Pulsante 2;&&&&L2G:0;}</{d5*15^B3%50,13r20!CCC:Pulsante 3;&&&&L3G:0;}</{d5*15^B4%50,13r20!CCC:Pulsante 4;&&&&L4G:0;}/*15^{*13T:SW1;*6WA0:0;}/{*13T:SW2;*6WB0:0;}/{*13T:SW3;*6WC0:0;}/{*13T:SW4;*6WD0:0;}");
         

  // Initialize digital output pin
  
  digitalWrite(2, 1);
  pinMode(2,OUTPUT);
  
  digitalWrite(3, 1);
  pinMode(3,OUTPUT);
  
  digitalWrite(4, 1);
  pinMode(4,OUTPUT);
  
  digitalWrite(5, 1);
  pinMode(5,OUTPUT);
  
  digitalWrite(6, 1);
  pinMode(6,OUTPUT);

  digitalWrite(7, 1);
  pinMode(7,OUTPUT);
  
  digitalWrite(8, 1);
  pinMode(8,OUTPUT);

  digitalWrite(9, 1);
  pinMode(9,OUTPUT);
  

  //inizializzazioni variabili locali
  StateMachine_B1=CMD_B1_WHITING;
  StateMachine_B2=CMD_B2_WHITING;
  StateMachine_B3=CMD_B3_WHITING;
  StateMachine_B4=CMD_B4_WHITING;
  
}

String Msg;

void loop() 
{
 
  int c;
  
  //---------------------------------
  //gestione e decodifica comandi da modulo wi-fi
  while ((c = Serial1.read()) > '\n') Msg += (char) c;  // Read incoming chars, if any, until new line
  if (c == '\n')                                       // is the message complete?
  { 
    if (Msg.equals("#B1P")) // Gestione pulsante 1
    {
      StateMachine_B1=CMD_B1_EXEC;
    }

    if (Msg.equals("#B2P")) // Gestione pulsante 2
    {
      StateMachine_B2=CMD_B2_EXEC;
    }

    if (Msg.equals("#B3P")) // Gestione pulsante 3
    {
      StateMachine_B3=CMD_B3_EXEC;
    }

    if (Msg.equals("#B4P")) // Gestione pulsante 4
    {
      StateMachine_B4=CMD_B4_EXEC;
    }
  
    //gestione switch

    if (Msg.equals("#WA1"))     {digitalWrite(5,0);}  //Serial.println("#L?0");}    // Turn ON LED ? and Relay if switch is ON 
    if (Msg.equals("#WA0"))     {digitalWrite(5,1);} //Serial.println("#L?1");}    // Turn OFF LED ? and Relay if switch is OFF

    if (Msg.equals("#WB1"))     {digitalWrite(4,0);} //Serial.println("#L?0");}   // Turn ON LED ? and Relay if switch is ON 
    if (Msg.equals("#WB0"))     {digitalWrite(4,1);} //Serial.println("#L?1");}   // Turn OFF LED ? and Relay if switch is OFF

    if (Msg.equals("#WC1"))     {digitalWrite(3,0);} //Serial.println("#L?0");}    // Turn ON LED ? and Relay if switch is ON 
    if (Msg.equals("#WC0"))     {digitalWrite(3,1);} //Serial.println("#L?1");}   // Turn OFF LED ? and Relay if switch is OFF
    
    if (Msg.equals("#WD1"))     {digitalWrite(2,0);} //Serial.println("#L?0");}    // Turn ON LED ? and Relay if switch is ON 
    if (Msg.equals("#WD0"))     {digitalWrite(2,1);} //Serial.println("#L?1");}    // Turn OFF LED ? and Relay if switch is OFF

       
    Msg = ""; //cancellazione comandi già processati

  } 

  //---------------------------------
  //gestione temporizzazioni LED1/OUT9
  switch(StateMachine_B1)
  {
    case CMD_B1_WHITING:   
          break;

    case CMD_B1_EXEC:
          digitalWrite(9,0); 
          Serial1.println("#L11");
          Timer_B1=10;                  //durata attivazione=1sec
          StateMachine_B1=CMD_B1_END;
          break;

    case CMD_B1_END:
          if(Timer_B1==0)
          {
            digitalWrite(9,1); 
            Serial1.println("#L10");
            StateMachine_B1=CMD_B1_WHITING;
          }
          break;
  }

  //---------------------------------
  //gestione temporizzazioni LED2/OUT8
  switch(StateMachine_B2)
  {
    case CMD_B2_WHITING:   
          break;
         
    case CMD_B2_EXEC:
          digitalWrite(8,0); 
          Serial1.println("#L21");
          Timer_B2=10;                  //durata attivazione=1sec
          StateMachine_B2=CMD_B2_END;
          break;

    case CMD_B2_END:
          if(Timer_B2==0)
          {
            digitalWrite(8,1); 
            Serial1.println("#L20");
            StateMachine_B2=CMD_B2_WHITING;
          }
          break;

  }

   //---------------------------------
  //gestione temporizzazioni LED3/OUT7
  switch(StateMachine_B3)
  {
    case CMD_B3_WHITING:   
          break;

    case CMD_B3_EXEC:
          digitalWrite(7,0); 
          Serial1.println("#L31");
          Timer_B3=10;                  //durata attivazione=1sec
          StateMachine_B3=CMD_B3_END;
          break;

    case CMD_B3_END:
          if(Timer_B3==0)
          {
            digitalWrite(7,1); 
            Serial1.println("#L30");
            StateMachine_B3=CMD_B3_WHITING;
          }
          break;
  }
  
 //---------------------------------
  //gestione temporizzazioni LED4/OUT6
  switch(StateMachine_B4)
  {
    case CMD_B4_WHITING:   
          break;

    case CMD_B4_EXEC:
          digitalWrite(6,0); 
          Serial1.println("#L41");
          Timer_B4=10;                  //durata attivazione=1sec
          StateMachine_B4=CMD_B4_END;
          break;

    case CMD_B4_END:
          if(Timer_B4==0)
          {
            digitalWrite(6,1); 
            Serial1.println("#L40");
            StateMachine_B4=CMD_B4_WHITING;
          }
          break;
  }

}


// **************************************************************************
// Interrupt Timer1 ogni 0,1sec
// **************************************************************************
ISR(TIMER1_COMPA_vect)          // timer compare interrupt service routine
{
    if(Timer_B1)   --Timer_B1;
    if(Timer_B2)   --Timer_B2;
    if(Timer_B3)   --Timer_B3;
    if(Timer_B4)   --Timer_B4;
}
Reply
#2
Ottimo lavoro.
Grazie per la condivisione.
Saluti
Enzo
Reply
#3
grazie per la condivisione, volevo chiedere se è possibile maggiori spiegazioni proprio sul programma in modo da agevolare anche chi come me è agli inizi e cerca di capire nel dettaglio il listato. grazie ancora
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)