I'm struggling a bit with my code..
The idea is that when the color sensor picks up a different value, it will then read the value and activate a relais.
So I need to save the first value it reads then as soon as it picks up another value the if statement will begin. I've already tried somethings, but I only have error codes.
Here's my code:
#define s0 8
#define s1 9
#define s2 10
#define s3 11
#define out 12
//GetColors();
int Red=0, Blue=0, Green=0;
void setup() {
//setup colorsensor
pinMode(s0,OUTPUT);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
pinMode(s3,OUTPUT);
pinMode(out,INPUT);
//setup relais
pinMode(6, OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
Serial.begin(9600);
digitalWrite(s0,HIGH);
digitalWrite(s1,HIGH);
}
void loop() {
sensor = pulseIn;
LastReading = ThisReading ;
ThisReading = pulseIn ;
if (ThisReading != pulseIn) When a different value is detected
{
if (Red <=15 && Green <=15 && Blue <=15) //now it will detect colors, this is white
{
colorcountERROR++;
}
else if (Red<Blue && Red<=Green && Red<23)
{
colorcountRed++;
}
else if (Blue<Green && Blue<Red && Blue<20)
{
colorcountBlue++;
}
else if (Green<Red && Green-Blue<= 8)
{
colorcountGreen++;
}
else //No color found
{
colorcountERROR++;
}
delay(500);
if (colorcountERROR >=9)
{
Serial.println ("Unknown");
}
else if (colorcountRed >=5)
{
Serial.println ("Red");
digitalWrite (6,HIGH);
}
else if (colorcountBlue >=5)
{
Serial.println ("Blue");
digitalWrite (5,HIGH);
}
else if (colorcountGreen >=5)
{
Serial.println ("Green");
digitalWrite (4,HIGH);
}
else
{
Serial.println ("wait..");
}
}
else //No difference in value
{
Serial.println ("Rad in stilstand");
}
}
void GetColors()
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
delay(20);
digitalWrite(s3, HIGH);
Blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
delay(20);
digitalWrite(s2, HIGH);
Green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
delay(20);
}
As for error codes; "Use of undeclared identifier 'sensor'clang(undeclared_var_use)","Use of undeclared identifier 'LastReading'clang(undeclared_var_use)", "Use of undeclared identifier 'ThisReading'clang(undeclared_var_use)".
these are found when comparing the previous value to the current value. this makes sense since I have yet to declare it, but I don't know how to do it properly.
All variables have to be appropriately declared. For example, if you intend to store the result of the pulseIn() function, then you look up the reference page for pulseIn()
to learn that it requires as arguments the correct digital pin number and a value (HIGH or LOW), and returns an unsigned long result.
So your code might look like this:
unsigned long sensor_value;
...
sensor_value = pulseIn(pin, HIGH);
BTW: Where did this code come from? It never could have worked, with lines like this:
To pick up the first value, you can read from the sensor in setup() and save those readings in global variables. Then, in loop(), you can read new values and compare them to those saved earlier.
Can you clarify what you mean by that? Do you mean a change in the colour or hue of the light, ignoring any change in the brightness level? Or should a change in the brightness also trigger the relay?
I'm pretty certain you don't want any change to trigger the relay. Even if the object in front of the sensor does not move and the light levels don't change, the readings from the sensor won't be exactly the same each time. There will always be some small random variations from reading to reading. You will need to spend some time observing the readings before you know how much of a change is a real change and not just a random fluctuation.
I want it to trigger when the color changes.
I have three colors I work with, Red, green and blue. when the color changes from what it picked up the moment the program runs, the if statement starts.
The three colors are on a 'wheel of fortune' so the starting value(color) might be different every time. and when the wheel stops spinning a relais must be activated. I've already coded that
The sensor outputs are square waves, so it should not matter if you choose HIGH or LOW because in theory the result should be the same. In practice, as I said, there will always be fluctuations in the readings.
Why would you use s2 or s3 with pulseIn() ? You declared these pins as OUTPUT.
The readings you will get for each colour on the wheel will vary depending on the ambient lighting, unless you can completely exclude ambient light from entering the sensor. Different types of ambient lighting will give different readings: sunlight different to overcast daylight different to florescent lighting different to led lighting...
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
Your OS and version can be valuable information, please include it along with extra security you are using.
Always list the version of the IDE you are using and the board version if applicable.
Use quote or add error messages as an attachment NOT a picture.