Pages: [1]   Go Down
Author Topic: Stop on black line  (Read 618 times)
0 Members and 1 Guest are viewing this topic.
Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm trying to get a motor to stop when my light sensor reads a black line. I'm also trying to have a screen display the value of the light sensor in real time. I'm using a dfrobot motor controller, SFE light sensor, and a Grove LCD. I have tested all the parts by themselves, and they work fine so I know I'm doing my program wrong. The program, no matter the value of the light sensor, always choses loop #2. I have no idea why. Any help would be appreciated.

Code:
#include <SerialLCD.h>
#if ARDUINO < 100
#include <NewSoftSerial.h> //this is a must
#else
#include <SoftwareSerial.h>
#endif

SerialLCD slcd(11,12);
int E2 = 5;                         
int M2 = 4;   
int sensorPin = A1;
int long sensorValue = 0;
int value = 250;
int value2 = 0;


void setup()
{

    pinMode(M2, OUTPUT);
    slcd.begin();
}

void loop()
{
  sensorValue = analogRead(sensorPin);

  while (sensorValue > 1050) {  // #1
   
  sensorValue = analogRead(sensorPin);
 
  slcd.setCursor(0, 1);
  slcd.print(sensorValue, DEC);
   
  digitalWrite(M2,HIGH);       
  analogWrite(E2, value);
   
  }
 
 
 
 while (sensorValue < 1050) {  // #2
   
    sensorValue = analogRead(sensorPin);
   
    slcd.setCursor(0, 1);
    slcd.print(sensorValue, DEC);
   
    digitalWrite(M2,HIGH);     
    analogWrite(E2, value2);   //PWM Speed Control
   
 }}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The maximum value from analogRead() is 1023 so it will ALWAYS be less than 1050.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3855
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good example of when it's useful to use Serial.print to send the value to the monitor for debugging....
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Good example of when it's useful to use Serial.print to send the value to the monitor for debugging....
And/or read the documentation for the functions being used.
Logged

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you sure about the value?

When the program starts up the light sensor reads 950, but once you move the sensor over black (or any other color than what it started on) the LCD displays bright as 9,500-9,900 and dark as 1011.

Any ideas?

Thanks,
Drew
Logged

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also I'm not using Serial.print as the lcd is telling me the value.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25807
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the LCD displays bright as 9,500-9,900
I'd suggest it reads "95", with a couple of leftover zeroes.
Yes, we're really sure about the analogRead.

Quote
Also I'm not using Serial.print as the lcd is telling me the value.
Which is why you should use debugged, well-understood methods to do your debug.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the LCD displays bright as 9,500-9,900 and dark as 1011.
I'm pretty sure that if you printed the value correctly on the LCD, it would show 950 to 990. But, without seeing your code...
Logged

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int long sensorValue = 0;
is it int or long or both?
Arduino will be mad... smiley-mr-green
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25807
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Arduino will be mad...
You sure about that?
Don't knock it until you've tried it   smiley-wink
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I think he must clear his LCD... I dont know whether his sketch may include  Lcd.clear() sort of function just next to the both of while loop or before printing any value..
1050 is strange... the first while loop will never work.. smiley-eek
Logged


Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all of your help. I should have tried the Serial.print from the beginning. The problem was that the lcd left the 0 from 1000 so it made it seem like the light sensor was reading 9900 when it was really 990.  I was able to adjust the values and it works now.

Thanks,
Drew


Code:
#include <SerialLCD.h>
#if ARDUINO < 100
#include <NewSoftSerial.h> //this is a must
#else
#include <SoftwareSerial.h>
#endif

SerialLCD slcd(11,12);
int E2 = 5;                         
int M2 = 4;   
int sensorPin = A1;
int long sensorValue = 0;
int value = 250;
int value2 = 0;


void setup()
{

    pinMode(M2, OUTPUT);
    slcd.begin();
}

void loop()
{
 
 
  sensorValue = analogRead(sensorPin);
 
  while (sensorValue < 1000) {  // #1
   
  sensorValue = analogRead(sensorPin);
 
  slcd.setCursor(0, 1);
  slcd.print(sensorValue, DEC);
   
  digitalWrite(M2,LOW);       
  analogWrite(E2, value);
   
  }
 
 
 
 while (sensorValue > 1000) {  // #2
   
    sensorValue = analogRead(sensorPin);
   
    slcd.setCursor(0, 1);
    slcd.print(sensorValue, DEC);
   
    digitalWrite(M2,HIGH);     
    analogWrite(E2, value2);   //PWM Speed Control
   
 }}
Logged

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I bit, this guy will comeback again.. He is like a pigeon, when the  smiley-kitty come near the pigeon ,he closes his eyes.. You solve the problem not hide it... smiley-draw
Okay, just try:
Code:
#include <SerialLCD.h>
#if ARDUINO < 100
#include <NewSoftSerial.h> //this is a must
#else
#include <SoftwareSerial.h>
#endif

SerialLCD slcd(11,12);
int E2 = 5;                         
int M2 = 4;   
int sensorPin = A1;
int sensorValue = 0;
int value = 250;
int value2 = 0;


void setup()
{

    pinMode(M2, OUTPUT);
    slcd.begin();
}

void loop()
{
 
 
  sensorValue = analogRead(sensorPin);
 
  while (sensorValue < 1023) {  // #1
   
  sensorValue = analogRead(sensorPin);
  slcd.clear();
  slcd.setCursor(0, 1);
  slcd.print(sensorValue, DEC);
   
  digitalWrite(M2,LOW);       
  analogWrite(E2, value);
   
  }
 
 
 
 while (sensorValue > 1023) {  // #2
   
    sensorValue = analogRead(sensorPin);
     slcd.clear();
    slcd.setCursor(0, 1);
    slcd.print(sensorValue, DEC);
   
    digitalWrite(M2,HIGH);     
    analogWrite(E2, value2);   //PWM Speed Control
   
 }}

« Last Edit: December 22, 2012, 12:41:58 pm by Khalid » Logged


Pages: [1]   Go Up
Jump to: