Go Down

Topic: contatore up/down con arduino (Read 2194 times) previous topic - next topic

casari

Feb 16, 2013, 12:04 pm Last Edit: Feb 16, 2013, 01:34 pm by casari Reason: 1
Salve a tutti, avrei un piccolo, ma grosso problema con uno skech di  arduino  :smiley-red: , in pratica a me servirebbe che il programma mi legga i tasti up/down, però al posto di darmi un'uscita diversa sempre accesa, me ne dia solo un'input che duri 50ms, cosa che questo programma non fa, perchè quando inizia, il led lampeggia all'infinito. Potete aiutarmi? di seguito ho postato lo skech:
Code: [Select]
#define BUTTON_DOWN 11
#define BUTTON_UP 12

int ledPinA =  6;    // LED connected to digital pin 3
int ledPinB =  9;    // LED connected to digital pin 4
int ledPinC =  4;    // LED connected to digital pin 5
int ledPinD =  3;    // LED connected to digital pin 6
int ledPinE =  2;    // LED connected to digital pin 7
int ledPinF =  7;    // LED connected to digital pin 8
int ledPinG =  8;    // LED connected to digital pin 9
int ledPinP =  5;   // LED connected to digital pin 10

int val_down = 0;
int old_val_down = 0;
int val_up = 0;
int old_val_up = 0;
int state = 0;
int gear = 1;

// The setup() method runs once, when the sketch starts

void setup()   {            
 pinMode(BUTTON_DOWN, INPUT);
 pinMode(BUTTON_UP, INPUT);
 
 // initialize the digital pin as an output:
 pinMode(ledPinA, OUTPUT);
 pinMode(ledPinB, OUTPUT);
 pinMode(ledPinC, OUTPUT);
 pinMode(ledPinD, OUTPUT);
 pinMode(ledPinE, OUTPUT);
 pinMode(ledPinF, OUTPUT);
 pinMode(ledPinG, OUTPUT);  
 pinMode(ledPinP, OUTPUT);
 
 // initialize serial communication:
 Serial.begin(9600);
}

void led0()
{
 digitalWrite(ledPinA, HIGH);
 delay(50);
 digitalWrite(ledPinA, LOW);
 delay(50);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, LOW);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, LOW);
}

void led1()
{
 digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, HIGH);
 delay(50);
 digitalWrite(ledPinB, LOW);
 delay(50);
 digitalWrite(ledPinC, LOW);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, LOW);
}

void led2()
{
 digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, HIGH);
 delay(50);
 digitalWrite(ledPinC, LOW);
 delay(50);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, LOW);
}

void led3()
{
 digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, HIGH);
 delay(50);
 digitalWrite(ledPinD, LOW);
 delay(50);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, LOW);
}

void led4()
{
digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, LOW);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, HIGH);
 delay(50);
 digitalWrite(ledPinE, LOW);
 delay(50);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, LOW);
}

void led5()
{
 digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, LOW);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, HIGH);
 delay(50);
 digitalWrite(ledPinF, LOW);
 delay(50);
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, LOW);
}

void led6()
{
 digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, LOW);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, HIGH);
 delay(50);
 digitalWrite(ledPinG, LOW);
 delay(50);
 digitalWrite(ledPinP, LOW);
}

void led7()
{
 digitalWrite(ledPinA, LOW);
 digitalWrite(ledPinB, LOW);
 digitalWrite(ledPinC, LOW);
 digitalWrite(ledPinD, LOW);
 digitalWrite(ledPinE, LOW);  
 digitalWrite(ledPinF, LOW);  
 digitalWrite(ledPinG, LOW);
 digitalWrite(ledPinP, HIGH);
 delay(50);
 digitalWrite(ledPinP, LOW);
 delay(50);
}



void setLed(int number)
{
 switch (number) {
   
   case 1:  
     led1();
     break;
   case 2:  
     led2();
     break;
   case 3:  
     led3();
     break;
   case 4:  
     led4();
     break;
   case 5:  
     led5();
     break;
   case 6:  
     led6();
     break;
   case 7:  
     led7();
     break;
     
   

 }
}
             
// the loop() method runs over and over again,
// as long as the Arduino has power

void oldloop()                    
{
                  // wait for a second
 led0();
 delay(1000);                  // wait for a second
 led1();
 delay(1000);                  // wait for a second
 led2();
 delay(1000);                  // wait for a second
 led3();
 delay(1000);                  // wait for a second
 led4();
 delay(1000);                  // wait for a second
 led5();
 delay(1000);                  // wait for a second
 led6();
 delay(1000);                  // wait for a second
 led7();
 delay(1000);                  // wait for a second
                  // wait for a second
}

void loop()
{
   val_up = digitalRead(BUTTON_UP);
   if ((val_up == HIGH) && (old_val_up == LOW)) {
     gear += 1;
     Serial.print("gear number:  ");
     Serial.println(gear, DEC);
     delay(100);
   }
   old_val_up = val_up;

   val_down = digitalRead(BUTTON_DOWN);
   if ((val_down == HIGH) && (old_val_down == LOW)) {
     gear -= 1;
     Serial.print("gear number:  ");
     Serial.println(gear, DEC);
     delay(100);
   }
   old_val_down = val_down;
   
   if (gear >= 6) {
     gear = 6;
   }
   if (gear <= 1) {
     gear = 1;
   }
   
   setLed(gear);
}

grazie a tutti..

uwefed

devi definiere una variablie di stato per ogni LED che metti a 1 quando il led ha lampeggiato e che viene messo a 0 quando cambi LED (incrementi o decrementi il contatore)

Ciao Uwe

casari

per quanto riguarda la parte di avanzare da un led ad un'altro quello funziona, l'unico problema è che funziona solo se il led, al suo turno si accende e rimane acceso, invece come è scritto adesso non funziona, nel senso che il led rimane lampeggiando all'infinito, e non è sempre che cambia da un led all'altro  :(

sciorty

Perchè la funzione in questa maniera viene eseguita ad ogni loop, non vorrei aver mal interpretato ma prova così:
Code: [Select]
#define BUTTON_DOWN 11
#define BUTTON_UP 12

int ledPinA =  6;    // LED connected to digital pin 3
int ledPinB =  9;    // LED connected to digital pin 4
int ledPinC =  4;    // LED connected to digital pin 5
int ledPinD =  3;    // LED connected to digital pin 6
int ledPinE =  2;    // LED connected to digital pin 7
int ledPinF =  7;    // LED connected to digital pin 8
int ledPinG =  8;    // LED connected to digital pin 9
int ledPinP =  5;   // LED connected to digital pin 10

int val_down = 0;
int old_val_down = 0;
int val_up = 0;
int old_val_up = 0;
int state = 0;
int gear = 1;

// The setup() method runs once, when the sketch starts

void setup()   {             
  pinMode(BUTTON_DOWN, INPUT);
  pinMode(BUTTON_UP, INPUT);
 
  // initialize the digital pin as an output:
  pinMode(ledPinA, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(ledPinC, OUTPUT);
  pinMode(ledPinD, OUTPUT);
  pinMode(ledPinE, OUTPUT);
  pinMode(ledPinF, OUTPUT);
  pinMode(ledPinG, OUTPUT); 
  pinMode(ledPinP, OUTPUT);
 
  // initialize serial communication:
  Serial.begin(9600);
}

void led0()
{
  digitalWrite(ledPinA, HIGH);
  delay(50);
  digitalWrite(ledPinA, LOW);
  delay(50);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, LOW);
}

void led1()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, HIGH);
  delay(50);
  digitalWrite(ledPinB, LOW);
  delay(50);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, LOW);
}

void led2()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, HIGH);
  delay(50);
  digitalWrite(ledPinC, LOW);
  delay(50);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, LOW);
}

void led3()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, HIGH);
  delay(50);
  digitalWrite(ledPinD, LOW);
  delay(50);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, LOW);
}

void led4()
{
digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, HIGH);
  delay(50);
  digitalWrite(ledPinE, LOW);
  delay(50); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, LOW);
}

void led5()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, HIGH);
  delay(50);
  digitalWrite(ledPinF, LOW);
  delay(50);
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, LOW);
}

void led6()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, HIGH);
  delay(50);
  digitalWrite(ledPinG, LOW);
  delay(50);
  digitalWrite(ledPinP, LOW);
}

void led7()
{
  digitalWrite(ledPinA, LOW);
  digitalWrite(ledPinB, LOW);
  digitalWrite(ledPinC, LOW);
  digitalWrite(ledPinD, LOW);
  digitalWrite(ledPinE, LOW); 
  digitalWrite(ledPinF, LOW); 
  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinP, HIGH);
  delay(50);
  digitalWrite(ledPinP, LOW);
  delay(50);
}



void setLed(int number)
{
  switch (number) {
   
    case 1: 
      led1();
      break;
    case 2: 
      led2();
      break;
    case 3: 
      led3();
      break;
    case 4: 
      led4();
      break;
    case 5: 
      led5();
      break;
    case 6: 
      led6();
      break;
    case 7: 
      led7();
      break;
     
     

  }
}
             
// the loop() method runs over and over again,
// as long as the Arduino has power

void oldloop()                     
{
                   // wait for a second
  led0();
  delay(1000);                  // wait for a second
  led1();
  delay(1000);                  // wait for a second
  led2();
  delay(1000);                  // wait for a second
  led3();
  delay(1000);                  // wait for a second
  led4();
  delay(1000);                  // wait for a second
  led5();
  delay(1000);                  // wait for a second
  led6();
  delay(1000);                  // wait for a second
  led7();
  delay(1000);                  // wait for a second
                   // wait for a second
}

void loop()
{
    val_up = digitalRead(BUTTON_UP);
    if ((val_up == HIGH) && (old_val_up == LOW)) {
      gear += 1;
      Serial.print("gear number:  ");
      Serial.println(gear, DEC);
      setLed(gear);
      delay(100);
    }
    old_val_up = val_up;

    val_down = digitalRead(BUTTON_DOWN);
    if ((val_down == HIGH) && (old_val_down == LOW)) {
      gear -= 1;
      Serial.print("gear number:  ");
      Serial.println(gear, DEC);
      setLed(gear);
      delay(100);
    }
    old_val_down = val_down;
   
    if (gear >= 6) {
      gear = 6;
    }
    if (gear <= 1) {
      gear = 1;
    }
   
   
}


(Ho messo la funzione dentro ogni blocco if dei pulsanti)

casari

scusami ma non ho capito bene cosa hai cambiato  :smiley-red:

casari

però funziona  XD grazie mille... cosa hai cambiato???  :D

sciorty

La funzione setled() (se non ricordo male), nel tuo codice veniva eseguita ad ogni loop:

Code: [Select]
loop:
se premo sopra
   aumenta gear

se premo sotto
   diminuisci gear

scrivi led()


Io invece ho cambiato così:

Code: [Select]
loop:
se premo sopra
   aumenta gear
   scrivi led()

se premo sotto
   diminuisci gear
   scrivi led()






casari

ho capito, grazie mille.. non so davvero come ringraziarti..  =(

sciorty


non so davvero come ringraziarti..  =(


basterebbe non essere tristi come quell'emoticon ahaha

casari

ahahahaha, grazie mille comunque  XD

Go Up