sketch on-off sequential

Arduino Hello, I bought two weeks duemilanove arduino, but I want to make a sequel with digital ports and two push buttons, one to turn on and the other off.

example:

Can anyone help put two buttons, one to turn on, and at the end to hang
Some can help me buttons do not work.

int switchon = 12;
int switchoff = 4;
int ledPin = 13;
int disc1 = 11;
int disc2 = 9;
int disc3 = 7;
int puxe = 5;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(disc1, OUTPUT);
pinMode(disc2, OUTPUT);
pinMode(disc3, OUTPUT);
pinMode(puxe, OUTPUT);
}
void loop ()
{
int leitura = digitalRead(switchon);
if (leitura == 0){
digitalWrite(switchon, 1);

}else{
digitalWrite(switchon, 0);
}
digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(disc1, HIGH);
delay(3000);
digitalWrite(disc2, HIGH);
delay(3000);
digitalWrite(disc3, HIGH);
delay(3000);
digitalWrite(puxe, HIGH);
delay(3000);

}

int switchPin = 12;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop ()
{
currentButton = debounce(lastButton);
if (lastButton == HIGH && currentButton == LOW)
{
ledOn = !ledOn;
}
lastButton = currentButton;
digitalWrite(ledPin, ledOn);
}

MauroArduino:
Arduino Hello, I bought two weeks duemilanove arduino, but I want to make a sequel with digital ports and two push buttons, one to turn on and the other off.

example:

Can anyone help put two buttons, one to turn on, and at the end to hang
Some can help me buttons do not work.

int switchon = 12;

int switchoff = 4;
int ledPin = 13;
int disc1 = 11;
int disc2 = 9;
int disc3 = 7;
int puxe = 5;
void setup()
{
  pinMode(switchon, INPUT);
  pinMode(switchoff, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(disc1, OUTPUT);
  pinMode(disc2, OUTPUT);
  pinMode(disc3, OUTPUT);
  pinMode(puxe, OUTPUT);
}
void loop ()
{
int leitura = digitalRead(switchon);
if (leitura == 0){
  digitalWrite(switchon, 1);

You've defined switchon as an input. How do you expect to write to it?

You've defined switchon as an input. How do you expect to write to it?

That's how you turn on or off the internal pullup resistors. In some cases, it's a perfectly reasonable thing to do.

This is not one of them, though.

OP, what were you trying to do there?

:blush:
Thanks Henry_Best and PaulS

Works

How do I turn off "My Swichoff"

"My switch on and off has 10K resistor, I learned that it is not necessary"

int switchon = 12;
int switchoff = 4;
int ledPin = 13;
int disc1 = 11;
int disc2 = 9;
int disc3 = 7;
int puxe = 5;
void setup()
{
pinMode(switchon, INPUT);
pinMode(switchoff, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(disc1, OUTPUT);
pinMode(disc2, OUTPUT);
pinMode(disc3, OUTPUT);
pinMode(puxe, OUTPUT);
}
void loop ()
{
int leitura = digitalRead(switchon);
if (leitura == 0){
digitalWrite(switchon, 1);

digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(disc1, HIGH);
delay(3000);
digitalWrite(disc2, HIGH);
delay(3000);
digitalWrite(disc3, HIGH);
delay(3000);
digitalWrite(puxe, HIGH);
delay(3000);
}}

int leitura = digitalRead(switchon);
if (leitura == 0){
  digitalWrite(switchon, 1);

Why are you diddling with the pullup resistor in loop()? In general, the state of the pullup resistor should be set in setup(), after the pinMode() statement. It should not be diddled with in loop().

There is no need to "turn off a switch". That happens when you release it.

}}

NEVER! Each } goes on its own line.

:slight_smile:

int ligar = 12;
int led = 13;
int disco1 = 11;
int disco2 = 9;
int disco3 = 7;
int puxe = 5;
void setup()
{
pinMode(ligar, INPUT);

pinMode(led, OUTPUT);
digitalWrite(led,HIGH);

pinMode(disco1, OUTPUT);
digitalWrite(disco1,LOW);

pinMode(disco2, OUTPUT);
digitalWrite(disco2,LOW);

pinMode(disco3, OUTPUT);
digitalWrite(disco3,LOW);

pinMode(puxe, OUTPUT);
digitalWrite(puxe,LOW);
}
void loop ()
{
int leitura = digitalRead(ligar);
if (leitura == 0){
digitalWrite(ligar, 1);

digitalWrite(led,HIGH);
delay(5000);

digitalWrite(disco1,HIGH);
delay(3000);

digitalWrite(disco2,HIGH);
delay(3000);

digitalWrite(disco3,HIGH);
delay(3000);

digitalWrite(puxe,HIGH);
delay(3000);
}
}

THe END

To turn off, RESET and GND
I thought I could use an input to turn off this sketch, at the end.

What do you mean by "turn off this sketch"?

wanted to pause and then resume functioning the sketch

MauroArduino:
wanted to pause and then resume functioning the sketch

You can accomplish that with a flag variable:

void loop()
{
  static int runCode = 0;

  if (runCode == 1)
  {
    // run your code
  }

  if (someConditionToResumeOccurs)
  {
    runCode = 1;
  }

  if (someConditionToStopOccurs)
  {
    runCode = 0;
  }
}

Of course, at best, using blocking code (delay() calls) may make it much less responsive than you'd wish. At worst, depending on the conditions to stop and resume, it may not work at all.

What kind of parameters I put here

if (someConditionToResumeOccurs)
if (someConditionToStopOccurs)

MauroArduino:
What kind of parameters I put here

if (someConditionToResumeOccurs)
if (someConditionToStopOccurs)

Whatever parameters you wish to make your sketch "pause" and "resume"

What is it that is going to pause and resume the program ?
A switch/button ?
Time passing ?
A value from an external sensor ?
Something else ?

I need an example with static int
switch, gives nothing

int ligar = 12;
int led = 13;
int disco1 = 11;
int disco2 = 9;
int disco3 = 7;
int puxe = 5;

void setup()
{
pinMode(ligar, INPUT);

pinMode(led, OUTPUT);
digitalWrite(led,HIGH);

pinMode(disco1, OUTPUT);
digitalWrite(disco1,LOW);

pinMode(disco2, OUTPUT);
digitalWrite(disco2,LOW);

pinMode(disco3, OUTPUT);
digitalWrite(disco3,LOW);

pinMode(puxe, OUTPUT);
digitalWrite(puxe,LOW);
}
void loop()
{
static int leitura = digitalRead(ligar);
if (leitura == 1);
digitalWrite(ligar, 0);
{
digitalWrite(led,HIGH);
delay(3000);

digitalWrite(disco1,HIGH);
delay(3000);

digitalWrite(disco2,HIGH);
delay(3000);

digitalWrite(disco3,HIGH);
delay(3000);

digitalWrite(puxe,HIGH);
delay(3000);
// run your code
}

if (digitalRead,ligar);
{
ligar = 1;
}

if (digitalRead,ligar);
{
ligar = 0;
}
}

  if (digitalRead,ligar);

Whatever you think this is doing, it isn't. Take a look at just about any examples on how to properly read digital pins.

Take a look at just about any examples on how to properly read digital pins.

and how to construct an if test in a way that works.

The input is 5v permanent, by clicking the button passes the 0v "read" then writes the outputs 5v

if (leitura == 1);

if you (properly) used { and } after every if statement, that would look like:

if (leitura == 1)
{
   ;
}

which is probably not what you want.

My sketch works
I can not use it

void loop()
{
static int runCode = 0;

if (runCode == 1)
{
// run your code
}

if (someConditionToResumeOccurs)
{
runCode = 1;
}

if (someConditionToStopOccurs)
{
runCode = 0;
}
}

do not know how to apply