Arduino Contol Fails

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.
}

Did you post your entire sketch? It looks like there is some missing code at the bottom.

Kindly provide me a quick solution to fix this.

I suggest that you fix it yourself. You have the hardware. We don't. We don't even know what hardware you have.

You have the software. You have the ability to add debug statements. You have the ability to determine where the failure occurs.

digitalWrite(13, High);

{ cough }

@David Thank you for quick response, Actually just forgot the last parenthesis, corrected that.

@Paul Nothing on hardware side, working on it from a week! My friend suggested try with external power , did that too but no luck.

@AWOL - Take cough syrup.

  sensorValue = analogRead(sensorPin);

What is getting stored in sensorValue? Why not print it, and find out?

  if(sensorValue>100) 
  {

Is sensorValue greater than 100? How do you know?

  else if(sensorValue<50)
  {

Is sensorValue less than 50? How do you know?

 else Serial.println("Fail!");

Why is a value between 50 and 100, inclusive, a fail?

AWOL - Take cough syrup.

How about you fix your code?

What is getting stored in sensorValue? Why not print it, and find out?

How about you read the code?

DavidOConnor:

What is getting stored in sensorValue? Why not print it, and find out?

How about you read the code?

There's a world of difference between sending something to a serial port, and actually printing it.

@Paul

The sensor is an LDR

  1. LDR never gives a 0 value so I have used the <0

  2. 100 is used so that it may detact the day light!

  3. Fail - condition is used to detect if LDR is functioning or not!

  4. 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.

DavidOConnor:

What is getting stored in sensorValue? Why not print it, and find out?

How about you read the code?

And just how does reading the code show what values are being returned from the analogRead()?

I have not seen in past couple of years someone advising me so dumb!!!

I will agree that there is one dumbass involved in this conversation. It isn't me.

abhijeetv2:
@Paul
The sensor is an LDR

Defined by you, earlier in the thread, as "Nothing on hardware side"

  1. 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.

  1. 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.

...R

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);

See reply #7

It has already been determined that the code doesn't compile, so why are we dancing around asking what values it cannot possibly print?

abhijeetv2:

  1. LDR never gives a 0 value so I have used the <0

What's that got to do with anything?

  1. How about you join a school?

Learn the Code or at least try to READ!

Oh dear.
Oh dear, oh dear.

Is there something more than this required?

As a matter of fact, there is. It is also necessary to share the results seen in the serial monitor.

DavidOConnor:

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.

...R