Hi all,
I am very new to arduino, I am using the code given below to control 5v dc motor with arduino.
Control works fine for first 1 or 2 tries then motor just keep on running irrespective of LDR sensorValue! ie controlling fails.
But when I do reset it works fine for just another couple of turns then again motor control fails.
Kindly provide me a quick solution to fix this.
Thank you.
int motorPin = 3;
int sensorPin = A0; // select the input pin for the ldr
unsigned int sensorValue = 0; // variable to store the value coming from the ldr
void setup()
{
pinMode(13, OUTPUT);
pinMode(motorPin, OUTPUT);
//Start Serial port
Serial.begin(9600); // start serial for output - for testing
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
// read the value from the ldr:
sensorValue = analogRead(sensorPin);
if(sensorValue>100)
{
digitalWrite(13, LOW); // set the LED off
analogWrite(motorPin, 0);
delay(1000);
}
else if(sensorValue<50)
{
digitalWrite(13, High); // set the LED on
analogWrite(motorPin, 250);
delay(1000);
}
else Serial.println("Fail!");
Serial.println(sensorValue);
delay(250); //short delay for faster response to light.
}
Fail - condition is used to detect if LDR is functioning or not!
How about you join a school?
Learn the Code or at least try to READ!
I have not seen in past couple of years someone advising me so dumb!!! So for future if you know nothing then atleast don't increase someone's hurdle more!
Can you, hand on heart, say that the code above compiles?
Because if it doesn't compile, it doesn't run and the description of behavior in your first post is garbage.
Defined by you, earlier in the thread, as "Nothing on hardware side"
LDR never gives a 0 value so I have used the <0
I gather you meant < 50, but it still doesn't answer the question of why values from 50 to 100 are considered a fail, but values from 0 to 49 are not. The other hadware, again part of the "Nothing on hardware side", is not shown. I can make an assumption that a LOW turns the motor on, and a HIGH (note spelling; not High, which causes your code to not compile) turns it off. If that's the case, your motor will run when sensorValue is >100, and if sensorValue drops to somewhere betwee 50 and 100 inclusive, it will not stop; and only print "Fail!". Your motor will only stop id the sensorValue is between 0 and 49.
How about you join a school?
I have not seen in past couple of years someone advising me so dumb!!! So for future if you know nothing then atleast don't increase someone's hurdle more!
You came here for help. No, wait, you came here for a handout of some quick code.
Help was given, and though you didn't like the tone used in giving it, it was inded help. Don't like it? Go somewhere else.
abhijeetv2:
I have not seen in past couple of years someone advising me so dumb!!!
I understand that some people use language here that runs people up the wrong way.
However, in my opinion, there is a simple question to ask yourself "Would the person offering the advice be easily able to get my project to work?
If the answer is YES (and it usually is) then the advice is useful, however roughly it may be expressed.
I don't consider myself anything special as a programmer, but I reckon I'm better than average at diagnosing problems. You will make NO progress with your problem until you insert code to show what values are actually being produced by the LDR. This was suggested to you by @PaulS twice.
You should also draw a diagram showing how everything is connected - including power supplies - and post a photo of the diagram. Many times the sort of symptoms you describe are due to an inadequate power supply.
until you insert code to show what values are actually being produced by the LDR.
Is there something more than this required? (line 41 in posted code)
Serial.println(sensorValue);
I confess I had not seen that. I tend to look at my values before I use them and when I didn't see a print statement before the IF statements I jumped to the conclusion that there was no print statement.
However, as @PaulS implies, the OP has been singularly secretive about the data provided by the print statement. Which also reinforced my incorrect assumption that there was no print statement.