Need Help on Programming

Hi Everyone.
I have started to learn Arduino programming .
I want to write a program and got doubt.
If I write a delay in the program it will pause the program in that state
Ex:- In a blink program if keep delay(10000); it is keeping the LED light ON for 10 seconds.
I want to Switch OFF in middle of the 10 seconds time.
How to do that? Can we use the interrupt command to switch OFF the light.

Below this is the problem:-
I have two push buttons- Button A and Button B.
3 LEd's - LED1, LED2, LED3

Initial state All THREE LEDs are in switch OFF mode.

I need to press the button only once and LED1 should switch ON for 20 seconds.

If i press two times continuously LED2 should switch ON and it should be ON for 20 Seconds,

If I press three times continuously then LED3 should Switch ON it should be ON for 20 Seconds.

In above all based on the press only one LED should Switch ON.

I have one more button (button2) , Its a emergency button.
When I press the Button2, it should switch OFF all the LEDs without any delay.

Please help me to write the code on this and give me a solution.

I tried debounce and interrupt command but could not able to write the code properly.

Thanks in Advance.

Use a millis() based approach. See e.g. the blink without delay example that comes with the ide, Using millis() for timing. A beginners guide and Demonstration code for several things at the same time.

Just to name a few :wink:

You need to think this through carefully because what you're trying to do is more complex than you seem to think.

The problem is that if you press 3 times quickly you will also have pressed 1 time and 2 times...you have to in order to get to the 3rd press. So when you press once you're going to have to wait a while to see if another press is going to come along and only if the second press doesn't arrive can you switch LED1 on.

And you really need to check for buttons going from not pressed to pressed rather than just being in a pressed state.

Steve

hi
I have taken some codes from the internet and tried to convert in the format.
Could you please check and tell me whether this code will work or I have written in correct format?

const byte LED_RED = 11;
const byte LED_GREEN = 12;
const byte LED_YELLOW = 13;
const byte Button1 = 2 ;
const byte Button2 = 3 ;
int count = 0;
boolean Button1;
boolean currentButton = false;
boolean LED_RED = false;
boolean LED_GREEN = false;
boolean LED_YELLOW = false;

void switchPressed ()
{
if (digitalRead (Button2) == HIGH)
digitalWrite (LED, LOW);
digitalWrite (LED, LOW);
digitalWrite (LED, LOW);

else
digitalWrite (LED, HIGH);
} // end of switchPressed

void setup ()
{
pinMode (LED_RED, OUTPUT); // so we can update the LED
pinMode (LED_GREEN, OUTPUT);
pinMode (LED_YELLOW, OUTPUT);
pinMode (Button1, INPUT)
pinMode(Button2,INPUT)
//digitalWrite (BUTTON, HIGH); // internal pull-up resistor
digitalWrite (LED_RED, LOW);
digitalWrite (LED_GREEN, LOW);
digitalWrite (LED_YELLOW, LOW);

attachInterrupt (digitalPinToInterrupt (Button1), switchPressed, CHANGE); // attach interrupt handler

boolean debounce(boolean last)
{
boolean current = digitalRead(Button2);
if (last != current)
{
delay(5);
current = digitalRead(Button2);
}
return current;

} // end of setup

void loop ()
{
// loop doing nothing
button1 = currentButton;
currentButton = debounce(button1);
if ( button1 == false && currentButton == true)
{
if (count == 0)
{
ledOn = !ledOn;
count++;
digitalWrite(LED_RED, ledOn);
delay(20000);
}

else if (count == 1)
{
ledOn = !ledOn;
count++;
digitalWrite(LED_GREEN, ledOn);
delay(20000);
}
else if (count == 2)
{
ledOn = !ledOn;
count = 0;
digitalWrite(LED_YELLOW, ledOn);
delay(20000);
}
}
}

No it won't work because it won't even compile.

Did you even try to compile it ?

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also use the AutoFormat tool to indent your code for easier reading.

...R

hackvivek:
I have taken some codes from the internet and tried to convert in the format.
Could you please check and tell me whether this code will work or I have written in correct format?

One of the problems that is keeping your sketch from compiling is the invisible characters that get put in your sketch when you copy text from a web page. You will have to hunt down each one and delete it.

Once those are gone, your first error is that you use the name Button1 for two different things. For pin names it is a good idea to end the variable name with the word 'Pin'. Change "const byte Button1 = 2 ;" to "const byte Button1Pin = 2;".

Auto-format your sketch. Then you will see why this gets you an "else without if" error:

  if (digitalRead (Button2) == HIGH)
    digitalWrite (LED, LOW);
  digitalWrite (LED, LOW);
  digitalWrite (LED, LOW);
  else
    digitalWrite (LED, HIGH);

Unlike in Python, indenting is not enough. If you want to put more than one statement into an 'if' you have to use a block statement:

  if (digitalRead (Button2) == HIGH)
  {
    digitalWrite (LED, LOW);
    digitalWrite (LED, LOW);
    digitalWrite (LED, LOW);
  }
  else
    digitalWrite (LED, HIGH);

Start with the topmost error and fix the first one you can fix, then re-verify and repeat until all errors are fixed. Once it compiles you can upload and run the sketch and then start figuring out why it doesn't do what you want it to.

hi
I could able to rewrite the code with help.
Yet my problem did not get solved.
I need to press only once then first led should glow.
If i press continuously for twice second LED only should glow.
If I press continuously thrice Third LED should glow.
I could able to write, First time if i press one time the first led glows, after the all LED becomes OFF. If i press twice only then second LED glows. I could not able to switch ON first or third LED by pressing thrice at one shot.
Can you please check the below code and help me?
//

const int ledPin1 = 11;
const int ledPin2 = 10;
const int ledPin3 = 9;
const int switchPin = 8; // momentary switch on pin no 8, other side connected to ground
const int Int_Switch = 2; // define interrupt pin to 2
volatile int state = LOW;
int count = 0;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pullup resistor
digitalWrite(Int_Switch, HIGH); // turn on pullup resistor
attachInterrupt(digitalPinToInterrupt(Int_Switch), interruptfunc, FALLING );
}

void loop()
{
if(digitalRead(switchPin) == 0)
{
delay(100);
if(digitalRead(switchPin) == 1)
{
count+=1;
switch(count)
{
case 1:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(5000);
digitalWrite(ledPin1, LOW);
break;
case 3:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
delay(5000);
digitalWrite(ledPin2, LOW);
break;
case 6:
count = 0;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(5000);
digitalWrite(ledPin3, LOW);
break;
default:break;
}
}
}
}
// ISR function
void interruptfunc()
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
count = 0;
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

This is the second time of asking.

...R

Here Robin so you do not have keep asking.
It took me two mouse clicks and copy to Arduino IDE .
It came out formated, hope you like it

const int ledPin1 = 11;
const int ledPin2 = 10;
const int ledPin3 = 9;
const int switchPin = 8;              // momentary switch on pin no 8, other side connected to ground
const int Int_Switch = 2;             // define interrupt pin to 2
volatile int state = LOW;            
int count = 0;


void setup()
{
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(switchPin, INPUT);
 digitalWrite(switchPin, HIGH);  // turn on pullup resistor
 digitalWrite(Int_Switch, HIGH); // turn on pullup resistor
 attachInterrupt(digitalPinToInterrupt(Int_Switch), interruptfunc, FALLING );
}

void loop()
{
 if(digitalRead(switchPin) == 0)
 {
   delay(100);    
   if(digitalRead(switchPin) == 1)
   {
     count+=1;
     switch(count)
     {
       case 1:
               digitalWrite(ledPin1, HIGH);
               digitalWrite(ledPin2, LOW);
               digitalWrite(ledPin3, LOW);
               delay(5000);
               digitalWrite(ledPin1, LOW);
               break;
       case 3:
               digitalWrite(ledPin1, LOW);
               digitalWrite(ledPin2, HIGH);
               digitalWrite(ledPin3, LOW);
               delay(5000);
               digitalWrite(ledPin2, LOW);
               break;
       case 6:
               count = 0;
               digitalWrite(ledPin1, LOW);
               digitalWrite(ledPin2, LOW);
               digitalWrite(ledPin3, HIGH);
               delay(5000);
               digitalWrite(ledPin3, LOW);
               break;
               default:break;                  
     }
   }
 }
}
// ISR function
void interruptfunc()
{
 digitalWrite(ledPin1, LOW);
 digitalWrite(ledPin2, LOW);
 digitalWrite(ledPin3, LOW);
 count = 0;
}

232:
Here Robin so you do not have keep asking.

Thank you.

However I was trying to make a point for the OP. Doing stuff for him that he is perfectly capable of doing himself is not constructive IMHO.

...R

UKHeliBob:
No it won't work because it won't even compile.

Did you even try to compile it ?

Are you implying that all is lost when code does not compile?
So how is OP going to learn how to debug and decode compiler error messages ?
I guess by properly posting formated code. How naive.

Please read the TITLE of this forum.

To the OP
Have you corrected the FIRST compiler error?

232:
I guess by properly posting formated code. How naive.

So, how is the OP going to fare in programming, if simple rules, designed to help others help, cannot be followed?

How naïve.

(In my book, debugging cannot commence until the code DOES compile)

232:
Are you implying that all is lost when code does not compile?

The OP asked

Could you please check and tell me whether this code will work

I answered

No it won't work because it won't even compile.

and asked

Did you even try to compile it ?

Had the OP tried to compile it they would have been aware that the code would not work.

This part will work for lighting a light for 5 seconds when count is 1, 3, or 6. I thought you wanted 1, 2, or 3.

      switch (count)
      {
        case 1:
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, LOW);
          delay(5000);
          digitalWrite(ledPin1, LOW);
          break;
        case 3:
          digitalWrite(ledPin1, LOW);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, LOW);
          delay(5000);
          digitalWrite(ledPin2, LOW);
          break;
        case 6:
          count = 0;
          digitalWrite(ledPin1, LOW);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, HIGH);
          delay(5000);
          digitalWrite(ledPin3, LOW);
          break;
        default: break;
      }

It will make the code easier to read if you make that a function and add comments:

const int ledPin1 = 11;
const int ledPin2 = 10;
const int ledPin3 = 9;
const int switchPin = 8;              // momentary switch on pin no 8, other side connected to ground
const int Int_Switch = 2;             // define interrupt pin to 2
//volatile int state = LOW;
volatile int count = 0;


void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  
  pinMode(switchPin, INPUT_PULLUP);  // turn on pullup resistor
  
  pinMode(Int_Switch, INPUT_PULLUP); // turn on pullup resistor
  attachInterrupt(digitalPinToInterrupt(Int_Switch), interruptfunc, FALLING );
}


void loop()
{
  // Do nothing until switch is closed
  if (digitalRead(switchPin) == 0)
  {
    delay(100);
    // If switch is open 100 milliseconds later...
    if (digitalRead(switchPin) == 1)
    {
      // Increment the counter
      count += 1;
      // Light a light if it matches the count
      lightForFiveSeconds(count);
    }
  }
}


void lightForFiveSeconds(int lightID)
{
  switch (lightID)
  {
    case 1:
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      delay(5000);
      digitalWrite(ledPin1, LOW);
      break;


    case 2:
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
      delay(5000);
      digitalWrite(ledPin2, LOW);
      break;


    case 3:
      count = 0;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH);
      delay(5000);
      digitalWrite(ledPin3, LOW);
      break;
  }
}


// ISR function
void interruptfunc()
{
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  count = 0;
}

In my book, debugging cannot commence until the code DOES compile.

Yes, in my book such approach defeats the purpose of complier to help by identifying SOME of the errors in the code. Not letting the complier to do so and waiting until the code is perfect "on paper" is timecosuming and builds a false sense of accomplishment - " I can do better than compiler ".
No thanks, I am too lazy NOT to use such valuable tool.

Perhaps yours and mine definitions of debugging differ, which is OK with me.

But this is wrong place for such "who cares anyway" discussion and is not helping the OP.

Sorry for OT.

Moderator edit:. Quote tags added to quote. Do please try to apply some basic courtesy.