Go Down

Topic: using a button to start the circuit (Read 785 times) previous topic - next topic

chevyboy

i need to use a NO button to start my circuit. i want to push the button once and have it run the sketch once then stop
i figured if i set the button as input on pin 12 for example and set up an if statement like....
if (pin12= HIGH);
{// put my sketch here}
but i keep getting errors, am i putting it in the wrong place? i've tried it in void setup and void loop, anyone have any suggestions? should i even be using an "if" here?

TeslaIaint

Please provide your entire code.

What kind of errors?

MarkT

Code: [Select]
if (pin12= HIGH);
{// put my sketch here}


Really? There are three obvious errors in one line of code there (some kind of record), = instead of ==, pin12 instead of digitalRead (pin12), and the blank statement after the if (the ';')  Don't you mean

Code: [Select]

 while (digitalRead (pin12) == LOW)
 {}
 my_stuff () ;


The sketch is always running, you have to wait for the event you are interested in.
[ I won't respond to messages, use the forum please ]

MarkT

All assuming your button is wired up appropriately ;)
[ I won't respond to messages, use the forum please ]

chevyboy

heres my sketch

int led = 13;
int button = 12;

void setup() {               

pinMode(led, OUTPUT);

digitalRead(button);
while(digitalRead (button)== HIGH);
{   
  digitalWrite(led, HIGH); 
  delay(1000);           
  digitalWrite(led, LOW);
  delay(500);
  digitalWrite(led, HIGH);
  delay(2000);
  digitalWrite(led, LOW);
}


void loop() {
}





heres my error message
whistle2.cpp: In function 'void setup()':
whistle2:29: error: a function-definition is not allowed here before '{' token
whistle2:30: error: expected `}' at end of input

MichaelMeissner

When quoting code, please use the '#' button to put a code tag around your code.  It allows us to see the code without additional formating.

In terms of your code, you are missing a close '}' in the setup function, which the compiler is actually telling you about.  In C/C+, all open braces ('{') must be matched by a close brace ('}').

TeslaIaint

#6
Oct 20, 2012, 03:10 am Last Edit: Oct 20, 2012, 04:30 am by TeslaIaint Reason: 1
Code: [Select]



int led = 13;
int button = 12;

void setup() {              

 pinMode(led, OUTPUT);


}


void loop() {

 digitalRead(button);
 if(digitalRead (button)== HIGH);    //edited to "if" from "while"
 {  
   digitalWrite(led, HIGH);
   delay(1000);          
   digitalWrite(led, LOW);
   delay(500);
   digitalWrite(led, HIGH);
   delay(2000);
   digitalWrite(led, LOW);
   digitalRead(button);
 }
}

Compiled but untested.
Do you see the difference? I also added the digital read in the while loop or else you just get stuck inside of it.
You can check that your brackets match by clicking next to them and making sure that it has an opposite.
Also use the auto-format function under tools (or CTRL-T). It helps your code to be more readable.

PeterH

I only provide help via the forum - please do not contact me for private consultancy.

Go Up