Switch isn't looping

Hey

I am a Student at the moment trying to program a switch.
However, everytime i push my switch button it loops the initial void setup, instead of the void loop.
Can someone help me please

int ledPin = 1;
void setup() {
pinMode(1, OUTPUT); // sets the ledPin to be an output
for(int x = 0; x < 3; x++)
{
digitalWrite(1, HIGH);
delay(500);
digitalWrite(1, LOW);
delay(500);
}
}
void loop() {
switch (5) {
case 1:
//High Powered LED light
digitalWrite (6, HIGH); //Turns the high Powered LED on
break;
default:
digitalWrite(6, LOW);
break;
}
}

Sounds to me like you're short circuiting the supply when you close the switch.
Please remember to use code tags when posting code

First off, please edit your post to have code tags. See How to use the forum.

Next, why do you think the program resets?

And my bed is on this

digitalWrite (6, HIGH);  //Turns the high Powered LED on

Not the code, but the (not shown) hardware. How did you connect the led? You can't connect a high power led directly to the Arduino. (I hope you're not a electrical engineering student if that's the case...)

I hope you don't think that this

      switch (5) {

has anything to do with your physical switch.

Ow wow, I didn't even notice :o But yeah, lack of code tags make that hard 0:-)

Hi again

Well, thanks for the quick replies.

Sorry if i have offended any of you for my lack of knowledge on Arduino...

I have used a LDR in connection with the High powered LED.
But the main problem i am having is when i press the switch button, it replays the initial flash 3 times when the system is turned on...

How do i change this to make the switch power the highPowered LED?

Thanks in advance...

Then please help use first. Change your post and give a schematic (Quick hand drawing will do just fine!) It's you who wants help so don't let us do all the work and not do anything with what we type.

(But a LDR in series with a high power LED sounds like trouble to me...)

My mistake I have a mosfet in connection with the high powered LED i used the wrong wording.
How do I go about programming the switch so it goes onto the case 1 section instead of re doing the initial flash 3 times?

int ledPin = 1;
void setup()    {    
      pinMode(1, OUTPUT);   // sets the ledPin to be an output
      for(int x = 0; x < 3; x++)
{
        digitalWrite(1, HIGH);
        delay(500);
        digitalWrite(1, LOW);
        delay(500);
      }
}
    void loop() {
      switch (5) {
        case 1:
        //High Powered LED light
        digitalWrite (6, HIGH);  //Turns the high Powered LED on
        break;
        default:
        digitalWrite(6, LOW);
      break;
      }
    }

But with that code the switch isn't going to reset it :wink: (because the loop does nothing)

So schematic please! It must be in the hardware. After that's fixed we can go over the program :slight_smile:

You ignored reply #3. There is nothing in your program that reads a physical switch. Let me guess, you have a switch attached to pin 5?

aarg:
You ignored reply #3. There is nothing in your program that reads a physical switch. Let me guess, you have a switch attached to pin 5?

Sorry didn't mean to,

If i take out the switch (5) then the program highlights case 1 as being incomplete without the switch section?

Xavier, you are really going to have to answer direct questions if you want any help around here. Please comply with reply #6. Without that information, it’s hard to guess what you are trying to do.

Let me rephrase
THE PROBLEM IS NOT IN THE CODE! :wink:
So you can change that all you want but it will not get you anywhere right now. Draw a schematic of the hardware (by hand), make a photo and post it here please.

I have attached a picture. I hope that suffices.

New to this forum as well, working with Xavier trying to figure out what to do, Firstly what is a code tag? also when we have put in

“switch (6) {” I’m guess it does nothing regarding the physical switch.

I this case would we used this coding for the switch:

"int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}

void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}"

Sorry to our amateur nature, have only been introduced to Arduino and its coding

Please have a look at this:
Tutorial/Button

ben.price96:
New to this forum as well

What are the three first thread titles in this forum?

xavier:
I hope that suffices.

No it does not. It's you who wants help, put some effort in it! Grab a pen and draw it! You might even see your own mistake.

@ben.price96, welcome to the forum. But reading How to use the forum applies to you as well :wink: That would explain all your questions about code tags. Once you know about them, please edit your post to have them.

And yeah, that code looks more like it. But again, problem now is NOT the code, it's the hardware.

Code tag, use the </> button, it opens a box to paste your code into

int ledPin = 13; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
 pinMode(ledPin, OUTPUT);  // declare LED as output
 pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
 val = digitalRead(inPin);  // read input value
 if (val == HIGH) {         // check if the input is HIGH (button released)
   digitalWrite(ledPin, LOW);  // turn LED OFF
 } else {
   digitalWrite(ledPin, HIGH);  // turn LED ON
 }
}

I would change these 2 lines, and wire the switch to connect the pin to Gnd when it is pressed:

pinMode(inPin, INPUT_PULLUP);    // declare pushbutton as input << with internal pullup resistor

 if (val == LOW) {         // check if the input is HIGH (button released)

That way the pin is always in a known state: HIGH from the internal pullup, or LOW from the switch being pressed.