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.
#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
}}
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.
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..
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
#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
}}
I bit, this guy will comeback again.. He is like a pigeon, when the come near the pigeon ,he closes his eyes.. You solve the problem not hide it... :%
Okay, just try:
#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
}}