Adding a button

I’m in need of any advice on adding a button to my project to control a LED light strip. I have already tried to incorporate it in my sketch with no luck, It does not break out of the loop . I was hoping by adding a button to pin one as well as adding an interrupt. Any help would be great.

#include “PCD8544.h”

// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
PCD8544 nokia = PCD8544(7, 6, 5, 4, 3);

int sensorPin = 0;

void setup() {
// Serial.begin(9600);
pinMode (11 , OUTPUT);
pinMode (10 , OUTPUT);
nokia.init();

nokia.setContrast(40);

nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYALLON);
delay(500);
// back to normal
nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYNORMAL);

nokia.clear();

}

void loop() {

nokia.display();

nokia.setCursor(0, 0);
nokia.print(“Kevin’s CNC “);
nokia.setCursor(0, 15);
nokia.print(” Controoler”);

int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
// Serial.print(temperatureF); Serial.println(" degrees F");
nokia.setCursor(10, 30);
nokia.print(temperatureF);
{

if (temperatureF >= 90 ){
digitalWrite (11,HIGH);
digitalWrite (10,HIGH); // led yellow to show warm
delay(250);
digitalWrite (10,LOW); // led yellow to show warm
delay(250);
nokia.setCursor(10, 40);
nokia.print(“TEMP RISING”);
}

else if (temperatureF <= 85) {
digitalWrite (10,LOW );
digitalWrite (11,LOW );
}

else
{
digitalWrite (11,LOW );
nokia.setCursor(10, 40);
nokia.print(" ");
}
}
}

Best to use the code tags when posting code (the # button). Makes it easier to read :slight_smile:

As for your question, if I’m assuming correctly you are wanting to stop the loop function from running?

The loop function will always run, and to stop it is to stop the micro controller from executing any more code (in essence, it’s probably a bit more than that). But if you only want to run the code you currently have in loop() when, say a button is pressed, you could wrap the code in an if statement.

Going by that assumption you would be looking at doing something like this. All you would need to do is hook a switch between pin 2 and GND.

And a side note, as I’ve just gone through trying to use a button on an external interrupt, it’s not really a good idea as the switch itself will change state numerous times quickly. I’m sure it can be done, but IMO its easier to just check in loop

#include "PCD8544.h"

// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
PCD8544 nokia = PCD8544(7, 6, 5, 4, 3);

int sensorPin = 0;

int buttonPin = 2;

void setup() {
  // Serial.begin(9600);
  pinMode (11 , OUTPUT);
  pinMode (10 , OUTPUT); 
  nokia.init();

  nokia.setContrast(40);

  nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYALLON);
  delay(500);
  // back to normal
  nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYNORMAL);

  nokia.clear(); 

  // Set the button pin to input (not generally needed but make it's intention clear)
  pinMode (buttonPin, INPUT); 
  // Enable internal pull up resistor
  digitalWrite(buttonPin, HIGH);

}

void loop() {

  if(digitalRead(buttonPin) == LOW)
  {
    nokia.display();

    nokia.setCursor(0, 0);
    nokia.print("Kevin's CNC ");
    nokia.setCursor(0, 15);
    nokia.print(" Controoler");

    int reading = analogRead(sensorPin); 
    float voltage = reading * 5.0;
    voltage /= 1024.0; 
    float temperatureC = (voltage - 0.5) * 100 ;
    float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
    // Serial.print(temperatureF); Serial.println(" degrees F");
    nokia.setCursor(10, 30);
    nokia.print(temperatureF);
    {
      if (temperatureF >= 90 ){
        digitalWrite (11,HIGH);
        digitalWrite (10,HIGH); // led yellow  to show warm
        delay(250);
        digitalWrite (10,LOW); // led yellow  to show warm
        delay(250);
        nokia.setCursor(10, 40);
        nokia.print("TEMP RISING");
      }


      else if (temperatureF <= 85) {
        digitalWrite (10,LOW );
        digitalWrite (11,LOW );
      }

      else 
      {   
        digitalWrite (11,LOW );
        nokia.setCursor(10, 40);
        nokia.print("           ");
      }
    }
  }
}

Hope this helps :slight_smile:

Thanks for the help. Would it work better to control the LED strip with a second controller?

whocares: Thanks for the help. Would it work better to control the LED strip with a second controller?

For such a simple task, it seems like overkill.