Help - don't know much about coding

I have this sketch. What I want it to do is, as long as the switch is on I want the program to loop ie keep the LED on but when the switch is OFF then the program should continue down to the next part.

Any help would be appreciated

void loop(){

buttonState = digitalRead(mistswitch); // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH) { //mister on // turn LED on: digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) }

if (buttonState==LOW){ digitalWrite(LED_BUILTIN,LOW);

}

Please read https://forum.arduino.cc/index.php?topic=148850.0 and pay special attention to point #7 about posting code.

Please post full code.

Please describe what the code currently does.

Something along the lines of

while (digitalRead(mistswitch) == HIGH)   //mister on
  { 
     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  }

You may need a pulldown resistor to keep the input in a LOW state until taken HIGH

if it's not HIGH, it's LOW...you don't need to test again with an if, just use an if-else construct

what you wrote matches what you described. When the button shows a HIGH it does one thing, when it's LOW it does the other thing.

it would help of course to know how the button is connected...


Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

.

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

nitesh1us:
I have this sketch.

That’s not a sketch, that’s what we call a snippet.

Thank you guys here is the full sketch. From " 3 Sensor 3 LEDs " down it works perfectly. Maybe not the the most efficient coding.

The problem is still the “Misterswitch” part which is an on off switch. (PIN 9 is pulled down.)
essentially the code should should keep the “Mister ON”(for now the in build LED) as long as the the switch is on and should not move down to the 3 switch 3 LED part of the code.

Right as it stands even if the misterswith is on I can still activate the push buttons as the code is moving down to the next section. There should be some code that has some return loop to see if the switch is off before it moves down.

Does that make sense?

/ INITIALIZATION
//************************************************************************************************************
// constants won’t change. They’re used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; //second button
const int buttonPin3 = 4; //3rd button
const int mister = 5; //output1
const int noise = 8; //output2
const int scary = 7; //output3
const int mistswitch = 9;//mister switch

const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);// second button
pinMode(buttonPin3, INPUT);// 3rd button
pinMode(mister, OUTPUT); //output1
pinMode(noise, OUTPUT); //output2
pinMode(scary, OUTPUT); //output3
pinMode(mistswitch, INPUT); // switch
}

//MISTER SWITCH
//************************************************************************************************************

void loop(){

while (digitalRead(mistswitch) == HIGH) //mister on
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
}

while (mistswitch == LOW);

digitalWrite(LED_BUILTIN, LOW);

// 3 SENSORS 3 LEDS
//***********************************************************************************************************

pinMode(LED_BUILTIN, OUTPUT);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(mister, HIGH); //mister on
delay(1000); // on board led timer
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(20000);//timer for mister -on
digitalWrite(mister,LOW); // Turn off mister
digitalWrite(noise, HIGH); //noise On
delay(20000);//timer for noise
digitalWrite(noise,LOW); // Turn off noise
digitalWrite(scary, HIGH); //scary On
delay(20000);//timer for scary
digitalWrite(scary,LOW); // Turn off scary

delay(10000); // delay 10 sec before retriggeer allowed(input)

}
buttonState = digitalRead(buttonPin2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(noise, HIGH); //noice Output
delay(1000); // on board led timer
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(20000);//noise ON delay
digitalWrite(noise,LOW); // Turn off noise
digitalWrite(scary, HIGH); //scary Output
delay(20000);//timer for scary
digitalWrite(scary,LOW); // Turn off scary
digitalWrite(mister, HIGH); //mister on
delay(20000);//timer for mister -on
digitalWrite(mister,LOW); // Turn off mister

delay(10000); // delay 10 sec before retriggeer allowed(input)

}

buttonState = digitalRead(buttonPin3);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(scary, HIGH); //scary Output
delay(1000); // on board led timer
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(20000);//timer for scary
digitalWrite(scary,LOW); // Turn off scary
digitalWrite(mister, HIGH); //mister on
delay(20000);//timer for mister -on
digitalWrite(mister,LOW); // Turn off mister
digitalWrite(noise, HIGH); //noice Output
delay(20000);//timer for scary
digitalWrite(noise,LOW); // Turn off scary

delay(10000); // delay x sec before retriggeer allowed(input)
}
}

This is the code formatted in the IDE using Auto format and posted here in code tags

//************************************************************************************************************
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; //second button
const int buttonPin3 = 4; //3rd button
const int mister = 5; //output1
const int noise = 8; //output2
const int scary = 7; //output3
const int mistswitch = 9;//mister switch

const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);// second button
  pinMode(buttonPin3, INPUT);// 3rd button
  pinMode(mister, OUTPUT); //output1
  pinMode(noise, OUTPUT); //output2
  pinMode(scary, OUTPUT); //output3
  pinMode(mistswitch, INPUT); // switch
}


//MISTER SWITCH
//************************************************************************************************************

void loop()
{
  while (digitalRead(mistswitch) == HIGH)   //mister on
  {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  }
  while (mistswitch == LOW);
  digitalWrite(LED_BUILTIN, LOW);
  // 3 SENSORS 3 LEDS
  //***********************************************************************************************************
  pinMode(LED_BUILTIN, OUTPUT);
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    // turn LED on:
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(mister, HIGH); //mister on
    delay(1000);                       // on board led timer
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(20000);//timer for mister -on
    digitalWrite(mister, LOW); // Turn off mister
    digitalWrite(noise, HIGH); //noise On
    delay(20000);//timer for noise
    digitalWrite(noise, LOW); // Turn off noise
    digitalWrite(scary, HIGH); //scary On
    delay(20000);//timer for scary
    digitalWrite(scary, LOW); // Turn off scary
    delay(10000); // delay 10 sec before retriggeer allowed(input)
  }
  buttonState = digitalRead(buttonPin2);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    // turn LED on:
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(noise, HIGH); //noice Output
    delay(1000);                       // on board led timer
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(20000);//noise ON delay
    digitalWrite(noise, LOW); // Turn off noise
    digitalWrite(scary, HIGH); //scary Output
    delay(20000);//timer for scary
    digitalWrite(scary, LOW); // Turn off scary
    digitalWrite(mister, HIGH); //mister on
    delay(20000);//timer for mister -on
    digitalWrite(mister, LOW); // Turn off mister
    delay(10000); // delay  10 sec before retriggeer allowed(input)
  }
  buttonState = digitalRead(buttonPin3);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    // turn LED on:
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(scary, HIGH); //scary Output
    delay(1000);                       // on board led timer
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(20000);//timer for scary
    digitalWrite(scary, LOW); // Turn off scary
    digitalWrite(mister, HIGH); //mister on
    delay(20000);//timer for mister -on
    digitalWrite(mister, LOW); // Turn off mister
    digitalWrite(noise, HIGH); //noice Output
    delay(20000);//timer for scary
    digitalWrite(noise, LOW); // Turn off scary
    delay(10000); // delay  x sec before retriggeer allowed(input)
  }
}

See how much easier it is to read ?

 while (digitalRead(mistswitch) == HIGH)   //mister on
  {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  }

When the switch is not closed what is preventing the input form floating to a HIGH value ?

const int mistswitch = 9;//mister switch
  while (mistswitch == LOW);

mistswitch will always have a value of 9 so will never be LOW. You need to read the state of the input. A second question about this line of code. Which code statement or statements should be executed if the test, when done properly, returns true ? As it is the only dependant code is the semicolon

Hi Newbee, here is a link with tips how to speed up getting good answers best regards Stefan

Hi nitesh1us,

What the other posts are trying to say is try putting your code in code tags.

You have your arduino code then when you reply, press the “</>” button in the upper left corner of your text editor and copy-paste your code in the code tags that appear. :slight_smile:

Or even easier, Auto format the code in the IDE then right click in the code and select Copy for Forum and paste it here with the code tags automatically added

How 'bout:

digitalWrite(LED_BUILTIN, digitalRead(mistswitch));

:)