Go Down

Topic: if pin 1 is high in 3seconds led will turn on else ignore .(if statement .help.) (Read 1 time) previous topic - next topic

PeterH

Your code:

Code: [Select]

if ( button, HIGH) {


Should be :

Code: [Select]

if(digitalRead(button) == HIGH) {


Your code:

Code: [Select]

x + 1;

Should be:
Code: [Select]

x = x + 1;


Your code layout makes it very difficult what your control structure is.

Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.
I only provide help via the forum - please do not contact me for private consultancy.

jaylisto


Your code:

Code: [Select]

if ( button, HIGH) {


Should be :

Code: [Select]

if(digitalRead(button) == HIGH) {


Your code:

Code: [Select]

x + 1;

Should be:
Code: [Select]

x = x + 1;


Your code layout makes it very difficult what your control structure is.

Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.



pls give me the answer coz my program didnt work. i repeat, there is no problem with the correct code. i only did that for representation (i know its wrong)

this is a very simple problem, though no one can answer even me.

the solution for my answer, i think...

check if the switch is high
delay (100)
check if the switch still high
delay (100)
check if the switch still high
delay (100)
check if the switch still high
turn on the led

but how can i transfer this to a code or something?.

if no one can help me then
ill keep trying to solve this problem.
jaylisto

PaulS

Quote
there is no problem with the correct code.

Then post that code.

jaylisto


it should be something along this line

Code: [Select]

if (Switch==HIGH)
{
x++;
if (x==3)
{
  digitalWrite(LED,HIGH);
  x=0;
}



}





woohooooooo. yes! it works! for all the answer out their. this is the only one made me smile  :smiley-mr-green:,
very good sir, thank you very much!. you only use looping but its the same with mine. what exactly is the one second as a delay? is it delay(1000)?
. you really help me sir. thank you very much.

Moderator edit: gibberish removed
jaylisto

jaylisto


Quote
there is no problem with the correct code.

Then post that code.


Code: [Select]
const int buttonPin = 2;    
const int ledPin =  11;
int x = 0;
int buttonState = 0;
void setup() {
 
 pinMode(ledPin, OUTPUT);      
 
 pinMode(buttonPin, INPUT);    
}

void loop(){
 
 buttonState = digitalRead(buttonPin);


 if (buttonState == HIGH) {
 x++;    
 
   delay(1000);// turn LED on:  
   
  if (x==3) {
         
   digitalWrite(ledPin, HIGH);
   x=0;
   
   }
 }
 
 
 else {
 
   digitalWrite(ledPin, LOW);
 }
}

jaylisto

Go Up