• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Problem z calym kodem
#1
Witajcie jestem poczatkujacym programista w arduino i nie tylko. Moj przelozony prosil mnie o zrobienie dla niego prostego programiku opierajacego sie na 6 ledach i 2 guzikach.
1 Guzik pierwsza sekwencja
2. Guzik druga sekwencja.
Generalnie taki jest kod, podlaczylem calosc jak powinna byc podlaczona kompiluje sie i flashuje sie. Lecz jest problem, nie dziala jak powinno. Moze jeszcze lepiej guziki wg nie wspolpracuja i nie robia tego co powinny, moglby ktos przeanalizowac moj kod? I napisac jakies sugesje czy cos?

Kod:
//Define all leds on which pins
#define leda 2
#define ledb 3
#define ledc 4
#define ledd 5
#define lede 6
#define ledf 7
#define buttona 12
#define buttonb 14

int ledPins = leda + ledb + ledc + ledd + lede + ledf;
int delayTime[] = {1000, 20000, 3000, 7000, 3000, 52000, 5000, 5000, 4000, 19000, 2000};
int pinCount = 6;

char seq1[] = {ledb, ledb, ledd, lede, lede, ledd, ledc, ledf, ledf ,ledc, leda};
char output1[] = {HIGH, LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW};
int time1[] =  {2000,20000,3000,7000,3000,52000,5000,5000,4000,19000,2000}; //11 +START
char seq2[] = {ledc, ledc, ledd, ledd, ledc, lede, lede, ledc, ledd, lede, lede, ledd, ledc, ledf, ledf, ledc, ledb};
char output2[] = {HIGH, LOW, HIGH, LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW};
int time2[] = {1000,9000,2000,8000,3000,7000,2000,18000,2000,2000,2000,29000,5000,5000,2000,19000,2000}; //18 + START
int buttons = buttona + buttonb;

byte buttonState;
byte lastState = LOW;
byte count = 0;

int program_running = 0; // 0 - no program running, 1 - first program running (firts button pressed), 2 - ....
int start_milis = 0;
int seq_step = 0;
int interval = millis() - start_milis;

void setup() {
 
 //Define all led and set all on OFF
pinMode(ledPins, OUTPUT);
 //Safe implementation

pinMode(buttona,INPUT);
pinMode(buttonb,INPUT);

}

void loop() {

 if( program_running == 1 ) {
   int interval = millis() - start_milis;
     sequence();
   
 }
 if( program_running == 2) {
   int interval = millis() - start_milis;
     sequence2();
   
 }


 //If pushbuttonA == sequence();
   if(digitalRead(buttona) == LOW)
   {
     program_running = 1;
     start_milis = millis();
         count = 1;
         sequence();
         ButtonCounter();
   }
 //If pushbuttonB == sequence();
     if(digitalRead(buttonb) == LOW)
     {
       program_running = 2;
       start_milis = millis();
         count = 1;        
         sequence2();
         ButtonCounter();
     }

}

void sequence(){
 //First sequence:
     digitalWrite(leda,HIGH);
     if(interval > 2000)
     {
       interval = 0;
       for(int forCount = 0; forCount < 12; forCount++)
       {
       
         if( interval > time1[seq_step])
         {
           digitalWrite(seq1[forCount],output1[forCount]);
           seq_step++;
           interval = 0;
         }
       };
     }
       
   //Complete sequence:
   digitalWrite(ledPins,HIGH);
 

  do
   {
     digitalWrite(ledPins,HIGH);
   }while(ledPins == HIGH); loop();

         
 //If push button twice time go to stoploop();
   // if(pushbutton == down && sequence == run())
}

void sequence2(){
       //Second sequence
     digitalWrite(ledb,HIGH);
     if(interval > 2000)
     {
       interval = 0;
       for(int forCountB = 0; forCountB < 12; forCountB++)
       {
       
         if( interval > time1[seq_step])
         {
           digitalWrite(seq2[forCountB],output2[forCountB]);
           seq_step++;
           interval = 0;
         }
       };
     }
       
  //Complete sequence:
 digitalWrite(ledPins,HIGH);

  do
   {
     digitalWrite(ledPins,HIGH);
   }while(ledPins == HIGH); loop();

 
 //If push button twice time go to stoploop();
  // if(pushbutton == down && sequence == run())
}
void stoploop(){

 //Stop sequence

 //Choose button and run sequence first or second
   //If pushbuttonA == sequence();
   if(digitalRead(buttona) == LOW)
   {
         count = 1;
         sequence();
         ButtonCounter();
   }
 //If pushbuttonB == sequence();
     if(digitalRead(buttonb) == LOW)
     {
         count = 1;
         sequence2();
         ButtonCounter();
     }

}

void ButtonCounter(){

 buttonState = digitalRead(buttons);
 if(buttonState == buttonState != lastState )
 {
   count++;
  if(count >= 2)
  do
  {
   //STOP sequence
   loop();
  } while(count >= 2);
 }


}
 
Odpowiedź
  


Wiadomości w tym wątku
Problem z calym kodem - przez firtusek - 18-05-2017, 15:27
RE: Problem z calym kodem - przez Robson Kerman - 18-05-2017, 19:15
RE: Problem z calym kodem - przez krn78 - 18-05-2017, 21:53
RE: Problem z calym kodem - przez firtusek - 19-05-2017, 14:13
RE: Problem z calym kodem - przez namok - 19-05-2017, 19:23
RE: Problem z calym kodem - przez Robson Kerman - 20-05-2017, 00:59

Skocz do:


Przeglądający: 1 gości