Controlling between a solid and blinking LED by one push of a button switch.

To Whom It May Concern,

I am having a difficult time programming what i assumed to be an easy task. I am using a strain gauge with a conditioner that is relaying voltage values to one of the analog inputs on the arduino. That is not the problem though i have the arduino programmed to trigger a relay and blinking LED when the weight limit/ voltage value has been met. I have integrated a button switch that when pressed a single time should cause the LED to stop blinking and remain ON. I am have a hard time getting my program to do this. Right now when the weight limit is reached the LED will blink however i have to hold the button switch down in order for the LED to stop blinking and remain solid. I need the LED to remain solid after a single push so i do not have to hold the button switch down. Any suggestions would be great.

Thanks,

Here is my code:

const int Conditioner = A0;    // pin that the conditioner is attached to
const int ledPin = 13;       // pin that the arduino LED is attached to
const int external_red = 9;       // pin that the external_red LED is attached to
const int relay_1 = 10;       // pin that the one channel relay is attached to
const int spec_weight = 125;   // an arbitrary value that's in the range of the analog input
const int blue_button_switch = 2;       // pin that the blue_button_switch is attached to
int blue_buttonState = 0;                // variable for reading the blue_button_switch status

void setup() {  
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
 // initialize the relay pin as an output:
 pinMode(relay_1, OUTPUT);
 // initialize the external_red pin as an output:
 pinMode(external_red, OUTPUT);
  // initialize the blue_button_switch pin as an input:
 pinMode(blue_button_switch, INPUT);
 // initialize serial communications:
 Serial.begin(9600);
}

void loop() {
 // read the value of the conditioner:
 int analogValue = analogRead(Conditioner);

 // if the analog value is high enough, turn on the LED and trigger the relay:
 if (analogValue >= spec_weight) {
   digitalWrite(ledPin, HIGH);         // sets the arduino LED on
   digitalWrite(relay_1, HIGH);        // triggers the channel one relay
 }/* else {
   digitalWrite(ledPin, LOW);
   digitalWrite(relay_1, LOW);
 }*/
 
 // when the analog value is high enough, cause the external_red led to blink until blue_button_switch is pressed:
 while (analogValue >= spec_weight) {
   blue_buttonState = digitalRead(blue_button_switch);
   // if the blue_button_switch is pressed, cause external_led to turn on:
   if (blue_buttonState == LOW){
     digitalWrite(external_red, HIGH);   // sets the external_red LED on
   } 
   //if the blue_button_switch is NOT pressed, cause external_led to blink: 
   else{
   digitalWrite(external_red, HIGH);   // sets the external_red LED on
   delay(500);                         // waits for a second
   digitalWrite(external_red, LOW);    // sets the external_red LED off
   delay(500);                         // waits for a second
   }
 } 


 // print the analog value:
 Serial.println(analogValue);
 delay(1);        // delay in between reads for stability
}

Right, let's get started then.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

BootyNinja:
I need the LED to remain solid after a single push so i do not have to hold the button switch down.

Have a look at the State Change Detection example.

Paul__B:
Right, let's get started then.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

You happy Paul_B? Haha
However it is good to know so thank you for that.

JimboZA:
Have a look at the State Change Detection example.

Thank you for the reply jimbo however i am using a button switch that is not attached to a breadboard it has a ground, signal, and a VCC. Therefor i am unable to wire it the same. As for the code i need it to blink before i press the button and stop blinking when i do. When i have tried similar code it wouldn't blink before i pressed it because it would run into the next command. I was able to make the led turn off and on by a single press but that is not what i want.

BootyNinja:
Thank you for the reply jimbo however i am using a button switch that is not attached to a breadboard it has a ground, signal, and a VCC.

Sounds like a "Keyes" module. You will therefore need to cite the Web link to that module in order for us to be able to advise on how to use it.

Paul__B:
Sounds like a "Keyes" module. You will therefore need to cite the Web link to that module in order for us to be able to advise on how to use it.

I have been unable to find any information on this button switch. It has YW-001 written on it and that is all i know about it.

It is simmilar to a keyes module.

It looks like this with a fancier button:

Look through this, I am sure you can pull out what you need and add it to your sketch:

//Blink without Delay skeleton 
//4 examples demonstrated
//LarryD

//LED wiring options
//=============================================

//#define PlusEqualsON //uncomment to invert

#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON  HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON  LOW
#define ledOFF HIGH

#endif

//=============================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long pin11Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 100UL;     //100ms
unsigned long ledOnTime      = 5*1000UL;  //5 seconds

byte laststartSwitchState    = HIGH;
byte buttonState             = HIGH;
byte counter                 = 0;

//the following are enable/disable flags
//some of these might not be used in this sketch
boolean flag13 = true;
boolean flag12 = true;
boolean flag11 = true;
boolean flag10 = true;

const byte startSwitch = 2; //pushed = LOW
const byte testSwitch  = 3; //pushed = LOW

//**********************************************************************

void setup()
{
  Serial.begin(9600);
  
  digitalWrite(13,ledOFF);
  pinMode(13, OUTPUT); 

  digitalWrite(12,ledOFF);
  pinMode(12, OUTPUT);

  digitalWrite(11,ledOFF);
  pinMode(11, OUTPUT);
  
  digitalWrite(10,ledOFF);
  pinMode(10, OUTPUT);

  pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
  pinMode(testSwitch,  INPUT_PULLUP); //pushed = LOW

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  //save the current time
  currentMillis = millis();

  //************************************* E x a m p l e  1
  //toggle pin 13 every 200mS
  //has 200ms or more gone by?
  if (currentMillis - pin13Millis >= 200UL)
  {
    //code here runs every 200ms
    //get ready for next iteration
    pin13Millis = pin13Millis + 200UL;
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }

  //************************************* E x a m p l e  2
  //at power up, pin 12 LED goes ON, after 3 seconds goes OFF and stays OFF
  //could be used as a powerup reset signal
  if (flag12 == true && currentMillis - pin12Millis <= 3000UL)
  {
    //code here runs for 3 seconds after power up, then stops
    digitalWrite(12,ledON);
  }
  else
  {
    digitalWrite(12,ledOFF);
    //disable further pin 12 control
    flag12 = false;
  }

  //************************************* E x a m p l e  3
  //if testSwitch is pushed and released
  //pin 11 LED goes ON for 5 seconds, then goes OFF 
  buttonState = digitalRead(testSwitch);
  
  //are we allowed to check the switch and is it pressed?
  if(flag11 == true && buttonState == LOW)
  {    
    //enable timing of LED on pin 11
    flag11 = false; //false --> timing is enabled
    //turn LED ON
    digitalWrite(11,ledON);
    //record the time LED turned ON
    pin11Millis = currentMillis;
  }
    
  //are we allowed and is it time to control pin 11
  if (flag11 == false && currentMillis - pin11Millis >= ledOnTime)
  {
    //if enabled, code here runs after ledOnTime ms goes by
    digitalWrite(11,ledOFF);
    //allow switch press detection again
    flag11 = true; //true --> switch monitoring is enabled
  }

  //************************************* E x a m p l e  4
  //is it time to check the switches?
  //in particular, pushing startSwitch will turn ON/OFF (toggle) an output pin 10
  //is it time to check the switches
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    //get ready for the next iteration
    SwitchMillis += debounceMillis; 
    //go and check the switches
    checkSwitches();    
  } 

  //*********************************
  //put other non-blocking stuff here
  //*********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;    

  //************************************* E x a m p l e  Push ON push OFF (toggle)   
  //check if this switch has changed state
  thisState = digitalRead(startSwitch);
  if (thisState != laststartSwitchState)
  {  
    //update the switch state
    laststartSwitchState = thisState;  

    //this switch position has changed so do some stuff

    //"HIGH condition code"
    //switch went from LOW to HIGH
    if(thisState == HIGH)        
    {
      //Do some HIGH switch stuff here
    }

    //"LOW condition code"
    //switch went from HIGH to LOW
    else                          
    {
      //Do some LOW switch stuff here  
      digitalWrite(10, !digitalRead(10));
      //print number of pushes
      counter++;
      Serial.println(counter);
    }

  } //END of startSwitch code

  //*****************************************  
  //similar code for other switches goes here 
  //*****************************************  

} //END of checkSwitches()

//**********************************************************************

//======================================================================
//                      E N D  O F  C O D E
//======================================================================

.

BootyNinja:
It looks like this with a fancier button:
http://www.banggood.com/Key-Switch-Sensor-Module-For-Arduino-p-76357.html

We may never know then, since the Banggood site is so unfriendly and has a completely useless search function.

LarryD:
Look through this, I am sure you can pull out what you need and add it to your sketch:

Thank you Larry I was able to achieve what I wanted using some of your example 2.

For anyone else here is the updated code:

const int Conditioner = A0;    // pin that the conditioner is attached to
const int ledPin = 13;       // pin that the arduino LED is attached to
const int external_red = 9;       // pin that the external_red LED is attached to
const int relay_1 = 10;       // pin that the one channel relay is attached to
const int spec_weight = 125;   // an arbitrary value that's in the range of the analog input
const int blue_button_switch = 2;       // pin that the blue_button_switch is attached to
int blue_buttonState = 0;                // variable for reading the blue_button_switch status

boolean flag9 = true;                    // enable/disable flag

void setup() {  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the relay pin as an output:
  pinMode(relay_1, OUTPUT);
  // initialize the external_red pin as an output:
  pinMode(external_red, OUTPUT);
   // initialize the blue_button_switch pin as an input:
  pinMode(blue_button_switch, INPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the conditioner:
  int analogValue = analogRead(Conditioner);

  // if the analog value is high enough, turn on the arduino LED and trigger the relay:
  if (analogValue >= spec_weight) {
    digitalWrite(ledPin, HIGH);         // sets the arduino LED on
    digitalWrite(relay_1, HIGH);        // triggers the channel one relay
  }/* else {
    digitalWrite(ledPin, LOW);
    digitalWrite(relay_1, LOW);
  }*/
  
  // when the analog value is high enough, cause the external_red led to blink until blue_button_switch is pressed:
  while (analogValue >= spec_weight) {
    blue_buttonState = digitalRead(blue_button_switch);
    // if the blue_button_switch has not been pressed, cause external_led to blink:
    if (flag9 == true && blue_buttonState == HIGH){
      digitalWrite(external_red, HIGH);   // sets the external_red LED on
      delay(50);                         // waits for a second
      digitalWrite(external_red, LOW);    // sets the external_red LED off
      delay(50);                         // waits for a second
    } 
    //if the blue_button_switch is pressed, cause external_led to stop blinking and remain on: 
    else{
      digitalWrite(external_red, HIGH);   // sets the external_red LED on
      flag9 = false;                    // disables further pin 9 control
    }delay(500);
  } 


  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}

Congratulations!

In the future plan your project using Robin2s tutorials:

http://forum.arduino.cc/index.php?topic=223286.0

http://forum.arduino.cc/index.php?topic=261445.0

.