I'm trying to make the motor and color sensor at the same time. I want to keep getting RGB value by the color sensor and the motor keeps working, after I press the button. But after I click the button, only the motor keeps working. And the Arduino only gets the RGB value when I click the button. It means I need to keep clicking the button to get data.
By any chance, could you please help me? Thanks in advance!
This is my code.
#define S0 5
#define S1 6
#define S2 7
#define S3 8
#define sensorOut 9
int redF = 0; int greenF = 0; int blueF = 0;
int redC = 0; int greenC = 0; int blueC = 0;
#include<IRremote.h>
int IRpin=11;
IRrecv IR(IRpin);
decode_results cmd;
String myCom;
int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed=255;
void setup()
{
Serial.begin(9600);
IR.enableIRIn();
pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
}
void loop(){
turntable();
color();
}
void turntable(){
while(IR.decode(&cmd)==0){
}
delay(1500);
IR.resume();
if (cmd.value==0xFFA25D){
myCom="pwr";
Serial.println(myCom);
}
if (cmd.value==0xFFE21D){
myCom="stop";
Serial.println(myCom);
}
if (cmd.value==0xFFE01F){
myCom="dn";
Serial.println(myCom);
}
if (cmd.value==0xFF906F){
myCom="up";
Serial.println(myCom);
}
if(myCom=="pwr"){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
analogWrite(speedPin,255);
}
if(myCom=="stop"){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
analogWrite(speedPin,0);
}
if(myCom=="up"){
mSpeed=mSpeed+15;
if(mSpeed>255){
mSpeed=255;
}
if(myCom=="dn"){
mSpeed=mSpeed-15;
if(mSpeed<0){
mSpeed=0;
}
analogWrite(speedPin,mSpeed);
}
}
}
void color(){
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
redF = pulseIn(sensorOut, LOW);
redC = map(redF, 70,120, 255,0);
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
greenF = pulseIn(sensorOut, LOW);
greenC = map(greenF, 100, 190, 255, 0);
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
blueF = pulseIn(sensorOut, LOW);
blueC = map(blueF, 38, 84, 255, 0);
Serial.print(redC); Serial.print(" ");
Serial.print(greenC); Serial.print(" ");
Serial.println(blueC);
}