Pages: [1]   Go Down
Author Topic: using a button to start the circuit  (Read 696 times)
0 Members and 1 Guest are viewing this topic.
ontario
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please provide your entire code.

What kind of errors?
Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11650
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
 while (digitalRead (pin12) == LOW)
  {}
  my_stuff () ;

The sketch is always running, you have to wait for the event you are interested in.
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11650
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All assuming your button is wired up appropriately smiley-wink
Logged

[ I won't respond to messages, use the forum please ]

ontario
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1829
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ('}').
Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:


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.
« Last Edit: October 19, 2012, 09:30:07 pm by TeslaIaint » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12523
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 while(digitalRead (button)== HIGH);

Hmmm.
Logged

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

Pages: [1]   Go Up
Jump to: