Pages: [1]   Go Down
Author Topic: Help with an led blink  (Read 709 times)
0 Members and 1 Guest are viewing this topic.
Toulouse / France
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!
I need some help. I am sure it must be simple.
I have a part of my code that troubles me a bit.
here it is :

Code:
void Sync() {
if(counter < maxi) {
counter++;
digitalWrite(13, stbout);
} else {
counter = 0;
digitalWrite(13, !stbout); 
}
}

dipending on what the program is doing, "stbout" is HIGH or LOW, but, as I am following a tempo (this is for midi application), when I speed up my midi sequencer, the LED blinks too fast to switch the circuitry it is supposed to switch.
Now my question is, is there a way to have a longer flash than the counter's flash time?
I am not sure to be understandable, so sorry but any help is greatly welcome.
Thanks
did
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PLease post the whole code as it is difficult to advice you on a single function.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26433
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could try capturing the time when you turn it on,  then when you see the time elapsed that you want you turn it off.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Toulouse / France
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry to be late, here is my code:
Code:
// midi data
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;

//compteur de clicks midi (24 click = 1 tps)
int counter = 0;

int stbout = 12; //bouton headrush
int stbout2 = 10;  //bouton headrush2
int vitbout = 6;  //bouton choix vitesse
int vitbout2 = 7;  //bouton choix vitesse
int rstbout = 8;  //bouton reset debut de mesure
int engagebout = 9; //deconnecte ou connecte a la HR
int maxi;  //variable pour temps maxi d'une mesure
int maxi2;  //variable pour temps maxi d'une mesure

//  variables pour le switch de choix de durée de la mesure (marche avec maxi)
long temps;
long debounce = 200;
long temps2;
long debounce2 = 200;
int previous = HIGH;
int previous2 = HIGH;
int midi_time = 0;
int midi_fois = 0;

// bouton de reset du debut de mesure
long rsttemps;
long rstdebounce = 200;
int rstprevious = HIGH;


void setup() {
Serial.begin(31250);
pinMode (6, INPUT);
digitalWrite (6,HIGH);
pinMode (7, INPUT);
digitalWrite (7,HIGH);
pinMode (8, INPUT);
digitalWrite (8,HIGH);
pinMode (9, INPUT);
digitalWrite (9,HIGH);
}

void loop() {

  stbout = digitalRead (12); //donne l'état du switch de la HR
  stbout2 = digitalRead (10); //donne l'état du switch de la HR
  vitesse_bouton(); //reglage du decoupage des temps
  vitesse_bouton2(); //reglage du decoupage des temps
  reset_bout(); //reset du premier blip
  engage(); //syncro on ou off
  if(Serial.available() > 0) {
    data = Serial.read();
      if(play_flag == 0){
        digitalWrite (13,stbout); // pour enclencher le transistor avant toute utilisation que la head rush marche meme dans les autres modes
        digitalWrite (11,stbout2);
      }
      if(data == midi_start) {
         play_flag = 1;
         digitalWrite (13,stbout);
         digitalWrite (11,stbout2);
      }
      else if(data == midi_continue) {
        play_flag = 1;
      }
      else if(data == midi_stop) {
        play_flag = 0;
        counter = 0;
        digitalWrite (13,stbout);
        digitalWrite (11,stbout2);
      }
      else if((data == midi_clock) && (play_flag == 1)) {

        Sync();
      }
  }
}




void Sync() {
if(counter < maxi) {
counter++;
digitalWrite(13, stbout);
digitalWrite (11,stbout2);
digitalWrite(A3, LOW);
} else {
counter = 0;
digitalWrite(13, !stbout); 
digitalWrite (11,!stbout2);
digitalWrite(A3, HIGH);
}

}



// a modifier selon convenance
void vitesse_bouton(){

vitbout = digitalRead(6);

if (vitbout == HIGH && previous == LOW && millis() - temps > debounce) {
  midi_time += 1;
  temps = millis();
}

if(midi_time == 0){
  maxi = 23;
}

if(midi_time == 1){
  maxi = 47;
  digitalWrite (2, HIGH);
}

if(midi_time == 2){
  maxi = 71;
  digitalWrite (3,HIGH);
}

if(midi_time == 3){
  maxi = 95;
  digitalWrite (4,HIGH);
}

if(midi_time == 4){
  midi_time = 0;
    digitalWrite (2,LOW);
    digitalWrite (3,LOW);
    digitalWrite (4,LOW);
}


previous = vitbout;



}
void vitesse_bouton2(){
  vitbout2 = digitalRead(7);
if (vitbout2 == HIGH && previous2 == LOW && millis() - temps2 > debounce2) {
  midi_fois += 1;
  temps2 = millis();
}

if(midi_fois == 0){
  maxi = maxi*1;
}

if(midi_fois == 1){
  maxi = maxi*2;
  digitalWrite (A0, HIGH);
}

if(midi_fois == 2){
  maxi = maxi*3;
  digitalWrite (A1,HIGH);
}

if(midi_fois == 3){
  maxi = maxi*4;
  digitalWrite (A2,HIGH);
}

if(midi_fois == 4){
  midi_fois = 0;
    digitalWrite (A0,LOW);
    digitalWrite (A1,LOW);
    digitalWrite (A2,LOW);
}
previous2 = vitbout2;
}

void reset_bout(){
  rstbout = digitalRead(8);
if (rstbout == HIGH && rstprevious == LOW && millis() - rsttemps > rstdebounce) {
  counter = 0;
  rsttemps = millis();
}
rstprevious = rstbout;
}

void engage(){
engagebout = digitalRead(9);
if (engagebout == HIGH){
  digitalWrite (13, stbout);
}
}

 
 
     

I would like to be able to control the time of the blinking led when counter = 0
thanks a lot
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26433
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So you only call Sync when serial.data comes in and it equals midi_clock and play flag is set.
midi_start has to happen, or midi_continue has to happen to set  play fllg first.

And those have to happen so many times before  your counter gets up to maxi.

I'm thinking you need some logic adjustment to let Sync be called more indepently?
Do you want it strictly time based, still tied in to the other two ... ?
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: