I’m using a TCS 34725, a bipolar stepper motor with a L293D.
I think I have a problem with the steps of the program.
If I’m in position “red” and the color “bleu” comes, it’s ok! But if it comes “bleu” again that turn 50 steps again but I want the stepper motor stay at the same place.
Bastos_Daniel:
If I’m in position “red” and the color “bleu” comes, it’s ok! But if it comes “bleu” again that turn 50 steps again but I want the stepper motor stay at the same place.
Well then, you’re going to have to tell the processor to store the last color somewhere and when a cycle begins and a new color is sensed compare it to the last color. If it’s the same as the last one, do nothing. If it’s different, do something. When a different color comes in don’t forget to store it as the old color. The concept is very similar to IDE → file/examples/digital/state change detection.
dougp:
Well then, you're going to have to tell the processor to store the last color somewhere and when a cycle begins and a new color is sensed compare it to the last color. If it's the same as the last one, do nothing. If it's different, do something. When a different color comes in don't forget to store it as the old color. The concept is very similar to IDE -> file/examples/digital/state change detection.
What dougp has proposed here should solve your problem...unless you have misstated your problem.
If I were doing it I would create a function that has the r, g, b as parameters and returns the colors ROUGE, BLEU, VERT, or NONE. You can then store off the color as dougp suggested and compare each time through loop() and only move the stepper if the color changes.
Could you explain why you are classifying the color twice?
It looks like:
RED does: nothing and then Back One Revolution
GREEN does: Forward Two Revolutions and then Forward One Revolution
BLUE does: Forward One Revolution and then nothing
This will move to a specific position based on the sensed color. Make sure the stepper is in the ‘rouge’ position when you turn on the Arduino unless you have a way to detect a ‘home’ position and add code to move to that position in setup().
#include <Stepper.h>
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
const int RougePosition = 0;
const int BleuPosition = 50;
const int VertPosition = 100;
int CurrentPosition = RougePosition;
Stepper myStepper(200, 8, 9, 10, 11);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
myStepper.setSpeed(60);
}
void loop() {
// put your main code here, to run repeatedly:
uint16_t r, g, b, c;
tcs.getRawData(&r, &g, &b, &c);
if (((r > 2000) && (r < 22000)) && (((g > 1200) && (g < 5800))) && ((b > 1000) && (b < 6500))) //rouge (vermelho)
{
Serial.println (F(" ROUGE "));
if (CurrentPosition != RougePosition)
{
myStepper.step(RougePosition - CurrentPosition);
CurrentPosition = RougePosition;
}
}
else if (((r > 2700) && (r < 12000)) && ((g > 4000) && (g < 28000)) && ((b > 6800) && (b < 34000))) //caminho do vermelho para o azul
{
Serial.println(F(" BLEU"));
if (CurrentPosition != BleuPosition)
{
myStepper.step(BleuPosition - CurrentPosition);
CurrentPosition = BleuPosition;
}
}
else if (((r > 7000) && (r < 13000)) && ((g > 20000) && (g < 35000)) && ((b > 10000) && (b < 15000))) //caminho do vermelho para o verde
{
Serial.print(F(" VERT"));
if (CurrentPosition != VertPosition)
{
myStepper.step(VertPosition - CurrentPosition);
CurrentPosition = VertPosition;
}
}
}