Leds and timing

Hello All!

I am trying to figure out, program, how to turn on the leds when the button is pressed quickly (eg. below 1 second), while I hold the button for a longer time (eg. 1 second) I fade the led. The button is on pin0 while the led is on pin 6.

What I have a the moment gives me on and off, and also fading, but when I fade when the led is on, it will firstly turn it off, and then fade… but I want that the fade continues from when I have left… of course if I have hold the button for two seconds… In addition, the fading including on and off is not stable in terms of right brightness, it blinks at some point :frowning:

Please have a look and please let me know, if you know how to fix this :confused:

Best.

void loop() {

  myBtn.update();    // call this every loop to update switch state

  myPrint();

  //above threshold time zones
  if (digitalRead(btn) == LOW ) {
    //start counting
    if (timeElapsed >= 10 && timeElapsed <= 1999) {
      // we are in short time zone
      timeShort = true;
      timeLong = false;
    }
    if (timeElapsed > 2000) {
      // we are in long time zone
      timeShort = false;
      timeLong = true;
    }
  }

  //MAKE DECISIONS
  // toggle the led (between on and off)
  // the led is off, then the fadeIn is true, turn it on
  if (digitalRead(btn) == LOW && timeShort) {
    //togle led state
    myBounce();

    if (ledState) {
      analogWrite(ledPin, brightness);
      ledOn = true;
      fadeIo = false;
      ledState = true;
    }
    if (!ledState) {
      analogWrite(ledPin, 0);
      ledOn = false;
      fadeIo = true;
      ledState = false;
    }    
  }
  else if(digitalRead(btn) == LOW && timeLong){

    // fading when led is on
    if (timeLong && ledOn) {
      fading();
    }
    // fading when led is on
    if (timeLong && !ledOn) {
      fading();
    }

  }

  //below threshold, reset the zones and timing
  if (digitalRead(btn) == HIGH) {
    //reset the timer
    timeElapsed = 0;
    //reset the ong timer (the short was already reset by creating the timerLong)
    timeLong = 0;
    timeShort=0;
  }
} //// end of loop


//FADING function
void fading() {

  //turn "off" the ledSig
  analogWrite(ledSigPin, 0);

  //fade up, if the led is off (brightness is 0), otherwise begin from where it was
  brightness = brightness + fadeAmount;

  //Serial.print("\t"); Serial.print(brightness); Serial.print("\t"); Serial.println(table[brightness]);  //go thru the table and use the value to fade the led
  //analogWrite(ledPin, pgm_read_byte(&table[brightness]));

  //Serial.print("\t"); Serial.print("Brightness:  "); Serial.print(brightness);
  
  if (brightness <= 1 || brightness >= 254) {
    fadeAmount = -fadeAmount;
    

  }
  analogWrite(ledPin, pgm_read_byte(&table[brightness]));
//  ledState = true;
//  ledOn = true;
  fadeIo = false;
}


void myPrint() {
  Serial.print("\t"); Serial.print(" btn: "); Serial.print(digitalRead(btn));
  Serial.print("\t"); Serial.print(" timeElap "); Serial.print(timeElapsed);
  Serial.print("\t");
  Serial.print("counter : "); Serial.print(timeElapsed);
  Serial.print("\t");
  Serial.print("timeShort : "); Serial.print(timeShort);
  Serial.print("\t");
  Serial.print("timerLong : "); Serial.print(timeLong);
  Serial.print(" Bri: ");
  Serial.print("\t");
  Serial.print(pgm_read_byte(&table[brightness]));
  Serial.print("\t");
  Serial.print(" ledState: "); Serial.print(ledState);
  Serial.print("\t");
  Serial.print(" ledOn: "); Serial.print(ledOn);
  Serial.print("\t");
  Serial.print(" fadeIo: "); Serial.println(fadeIo);


// fading table

const PROGMEM byte table[] =
{
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  3, 3, 3, 3, 3, 3, 3,
  4, 4, 4, 4, 4,
  5, 5, 5, 5,
  6, 6, 6, 6,
  7, 7, 7,
  8, 8, 8,
  9, 9,
  10, 10,
  11, 11,
  12, 12,
  13, 13,
  14, 14,
  15,
  16,
  17, 17,

  18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 36, 37, 39, 41,
  42, 44, 46, 47, 49, 51, 53, 55, 57, 59, 62, 64, 66, 69, 71, 73, 76, 79, 81, 84,
  87, 89, 92, 95, 98,

  101, 104, 106, 109, 112, 115, 118, 121, 124, 128, 131, 134, 137, 140, 143, 146, 149,
  151, 154, 157, 160, 163, 166, 168, 171, 174, 176, 179, 182, 184, 186, 189, 191, 193,
  196, 198, 200, 202, 204, 206, 208, 209, 211, 213, 214, 216, 218, 219, 221, 222, 223,
  225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 238, 239, 240,
  241, 241,
  242, 242,
  243, 243,
  244, 244,
  245, 245,
  246, 246,
  247, 247, 247,
  248, 248, 248,
  249, 249, 249, 249,
  250, 250, 250, 250,
  251, 251, 251, 251, 251,
  252, 252, 252, 252, 252, 252, 252,
  253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253,
  254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254
};

mu234:
What I have a the moment gives me on and off, and also fading, but when I fade when the led is on, it will firstly turn it off, and then fade…

//FADING function
void fading() {

  //turn "off" the ledSig
  analogWrite(ledSigPin, 0);     <<=== you turn it off here.  don't do that if that's not what you want.

mu234:
The button is on pin0

What kind of Arduino? On most Arduino, you should not use pin 0. It is used for sketch upload and serial monitor. Same for pin 1.

Using Arduino UNO... so far, at least that I would know, I do not have the problems for reading ... :slight_smile: but, thank you anyway!

Best.

So the program has to:
Use a led and a button
When button is not pressed the led is off
If the button is pressed:

  1. the first second the sed is just on
  2. after the led has to fade

Is it correct?

Silente:
Is it correct?

Is what correct? The code you posted before? As we told you, it isn't. Or the code you have now? How can we know if you don't share it?

But witch code?

Silente:
But witch code?

No, don't rely on spells or black magic to make you code work. Eye of frog and wing of bat are incompatible with Arduino logic levels (and cruel to animals).

I repeat, whitch is the code you are talking about? I don't remember I posted any code here