How to make the motor work and get data by color sensor at the same time?

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);
}

I guess this while loop is the problem:

Thank you for pointing the problem. I'm very new, so I have no idea how to solve the problem. Could you let me know how to deal with this problem?

I deleted the cod(while(IR.decode(&cmd)==0). But I didn't solve the problem.

Edit your post and insert code tags!

Try to replace it by

if (IR.decode(&cmd) == 0) return;

You might have to limit the serial output because now you'll get a lot of it.

Thank you so much!!! It's working.

I'm sorry to bother you again. But I'd like to know how to stop the sensor from getting value after I click the stop button?

I used this cod, but it's not working.
if(mSpeed==0){
redF = pulseIn(sensorOut, 0);
redC = map(redF, 0,0, 0,0);
greenF = pulseIn(sensorOut, 0);
greenC = map(greenF, 0, 0, 0, 0);
blueF = pulseIn(sensorOut, 0);
blueC = map(blueF, 0, 0, 0, 0);
}

Still no code tags!

You'd need to use the code that tells it what to do when you push the button to start it. This time tell it to stop if you pressed the button. My suggestion is program a start button and a stop button. You have a line in your code that says to delay then resume. use the correct terminology for the stop button have the code check if the stop button was pressed at the end of each loop cycle.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.