my first project: led blinks contentiously i want it to blink 2 or 3 times only

i am making a project with light senser on arduino uno; when light increases the led goes on and blinks when light light decreases it goes off …HELP;; the led blinks contentiously i want it to blink 2 or 3 times only ,…any suggestions…

////////Define Pins
int sensepin = 0;
int ledpin =13;
void setup(){

analogReference(DEFAULT);
pinMode (ledpin,OUTPUT );
//The LED pin needs to be set as an output
Serial.begin(9600);
}

void loop()
{
for (int i_count=0; i_count <=2; i_count++)
analogWrite(ledpin,i_count);
delay(50);

int val =analogRead(sensepin);
delay(1000);
Serial.println(analogRead(sensepin));
if(val<70)digitalWrite(ledpin,LOW);
else digitalWrite (ledpin,HIGH);
delay(500);
}

Its probably because you are using a contentious LED

Duane B

rcarduino.blogspot.com

any minor changes in code could solve the problem as it is my first project

If you only want something to happen once, put all your code in "setup"

Please remember to use code tags when posting code

i am making a project with light senser on arduino uno;

int ledpin =13;
   for (int i_count=0; i_count <=2; i_count++)
      analogWrite(ledpin,i_count);
      delay(50);

analogWrite() only works on PWM pins. On a Uno, pin 13 is NOT a PWM pin.

Why are you writing 0, 1, and 2 to the pin anyway? The pin will barely start flowing any current - not enough to light an LED enough to be seen.

How long do you think it will take the for loop to complete? How long will the pin be at each step?

You seem to have a misunderstanding of the extent of a for loop, and of the fact that loop() is called again and again.

actually i am making a project in which arduino will sense the light from a mobile phone when a incoming call is there and will dial another phone whose latest dialed number will be dialed in two pulses
by the relay ,,,,code in "setup" will work once i want it to sense the light all the time as it will be placed the phone in home unattested ..any suggestions

love7007:
actually i am making a project in which arduino will sense the light from a mobile phone when a incoming call is there and will dial another phone whose latest dialed number will be dialed in two pulses
by the relay ..any suggestions

And where does blinking LEDs come into play?

actually i am making a project in which arduino will sense the light from a mobile phone when a incoming call is there and will dial another phone whose latest dialed number will be dialed in two pulses
by the relay ,,,,code in "setup" will work once i want it to sense the light all the time as it will be placed the phone in home unattested ..any suggestions

love7007:
actually i am making a project in which arduino will sense the light from a mobile phone when a incoming call is there and will dial another phone whose latest dialed number will be dialed in two pulses
by the relay ,,,,code in "setup" will work once i want it to sense the light all the time as it will be placed the phone in home unattested ..any suggestions

So only perform the action after you "sense the light"

i'm not sure i understood what you are trying.
But if i understood it right, the led keeps blinking constantly because you have the for loop inside your void loop, so each time the void loop runs it will also run your void loop.

but to keep it very simple, you could make a new void with just the blinking code, can then "call it" when some kind of event happens. something like:

void blinkTwice() {
   digitalWrite(ledPin,HIGH);
   delay(50);
   digitalWrite(ledPin,LOW);
   delay(50);
   digitalWrite(ledPin,HIGH);
   delay(50);
   digitalWrite(ledPin,LOW);
}

void loop() {
   if (your condition for the blinking) {
      blinkTwice();   
   }
}

This would mean that each time your if condition is met (only when it is met), the void blinkTwice will run, and so the led will blink twice.

There are surelly more "elegant" ways of doing it, but maybe you get the idea...
:wink:

dear this is not working

////////Define Pins
int sensepin = 0;
int ledpin =13;

void setup(){

analogReference(DEFAULT);
pinMode (ledpin,OUTPUT );
//The LED pin needs to be set as an output
Serial.begin(9600);
}
void blinkTwice ( ) {
digitalWrite(ledpin,HIGH);
delay(50);
digitalWrite(ledpin,LOW);
delay(50);
digitalWrite(ledpin,HIGH);
delay(50);
digitalWrite(ledpin,LOW);
delay(50);

}

void loop()

{
int val =analogRead(sensepin);

if (val<70)digitalWrite(ledpin,LOW);
else digitalWrite ( ledpin,HIGH); // THIS WORKS
// else digitalWrite (ledpin,blinkTwice); // THIS LINE DOES NOT WORK
delay(500);
Serial.println(analogRead(sensepin));
}

this is not working

Try offering a pension or medical plan

//  else digitalWrite (ledpin,blinkTwice);

Unless blinkTwice evaluates to HIGH or LOW, what do you expect it to do?

Please start using code tags when posting code.

love7007:
// else digitalWrite (ledpin,blinkTwice); // THIS LINE DOES NOT WORK

You have that line commented out, so it would never work!!
Those "//" at the begining of the line mean that that line will be ignored.

I am not sure what is it you want to do in this project. Could you explain it better?
Did i understand right that you want the LEDs to blink when the light increases (when you are reading the light from pin A0?) ?

If so, you could try something like this:

////////Define Pins
int sensePin = A0;
int ledPin = 13;
int lastVal = 0;

void setup(){
  pinMode (ledpin,OUTPUT);
  Serial.begin(9600);
}

void loop(){ 
  int val = analogRead(sensePin);
  
  if (val > lastVal) {   // if the current reading is bigger than the last Reading...
    Serial.println(val); // print in the console the value of current reading
    blinkTwice();        // execute blinkTwice
  }
  
  lastVal = val;         // current Reading becomes now the last Reading
  delay(500);            // pause for 500ms (half a second)
                         // after the pause the program will check again if (val > lastVal)
}

void blinkTwice ( ) {    // this will run only when the current Reading is bigger than the last Reading
   digitalWrite(ledpin,HIGH);
   delay(50);
   digitalWrite(ledpin,LOW);
   delay(50);
   digitalWrite(ledpin,HIGH);
   delay(50);
   digitalWrite(ledpin,LOW);
   delay(50);
}

What would you like to happen when the light did NOT increase?

Did this help you?

Here is a version of your code with minor tweaks and omissions.

////////Define Pins
int sensepin = 0;
int ledpin =13;

void setup() {
  analogReference(DEFAULT);
  pinMode (ledpin, OUTPUT);  //The LED pin needs to be set as an output
  Serial.begin(9600);
}

void blinkTwice ( ) {
  digitalWrite(ledpin,HIGH);
  delay(50);
  digitalWrite(ledpin,LOW);
  delay(50);
  digitalWrite(ledpin,HIGH);
  delay(50);
  digitalWrite(ledpin,LOW);
  delay(50);
}

void loop() {
  int val = analogRead(sensepin);
  if (val < 70) { //what are you reading??? should it be (val > 70)?
    blinkTwice();
    delay(500);
    Serial.println (val);
  }
}

Dear thanks v.v.much ,it is working perfectly but it should stop blinking after 2 blinks, actually i will place my phone on the light senser ,when any call comes to my phone it will light the senser on and will give command through relay to another phone twice; once to activate it and second blink will call the last redial call in the phone, i hope you might have understood it ....

yes it should it be (val > 70)

but it should stop blinking after 2 blinks,

So put the code in "setup".
I think this has already been suggested.