Pages: [1]   Go Down
Author Topic: contatore up/down con arduino  (Read 1698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
broddoyyyyyyyyyyyyyyyyy
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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..
« Last Edit: February 16, 2013, 07:34:36 am by casari » Logged

BZ (I)
Offline Offline
Brattain Member
*****
Karma: 266
Posts: 21671
+39 349 2158303
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
broddoyyyyyyyyyyyyyyyyy
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-sad
Logged

Capo d'Orlando
Offline Offline
God Member
*****
Karma: 2
Posts: 725
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perchè la funzione in questa maniera viene eseguita ad ogni loop, non vorrei aver mal interpretato ma prova così:
Code:
#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)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
broddoyyyyyyyyyyyyyyyyy
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 15
broddoyyyyyyyyyyyyyyyyy
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

però funziona  smiley-lol grazie mille... cosa hai cambiato???  smiley-grin
Logged

Capo d'Orlando
Offline Offline
God Member
*****
Karma: 2
Posts: 725
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
loop:
 se premo sopra
   aumenta gear

 se premo sotto
   diminuisci gear

 scrivi led()

Io invece ho cambiato così:

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

 se premo sotto
   diminuisci gear
   scrivi led()

 


 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
broddoyyyyyyyyyyyyyyyyy
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ho capito, grazie mille.. non so davvero come ringraziarti..  smiley-cry
Logged

Capo d'Orlando
Offline Offline
God Member
*****
Karma: 2
Posts: 725
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

non so davvero come ringraziarti..  smiley-cry

basterebbe non essere tristi come quell'emoticon ahaha
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
broddoyyyyyyyyyyyyyyyyy
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ahahahaha, grazie mille comunque  smiley-lol
Logged

Pages: [1]   Go Up
Jump to: