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);
}
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
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...
////////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));
}
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?
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 ....