How to stop and start the sensor getting data by remote controller?

Hi, there! I'm new here. I want to stop and start the color sensor getting RGB value by the remote controller. But I found my cod is not working. By any chance, could you help me solve the problem? Thanks in advance!

This is my cod.

#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()
{

  setupTurntable ();
  setupColor ();
}
  


void setupTurntable (){

  Serial.begin(9600);
  IR.enableIRIn();

  pinMode(speedPin,OUTPUT);
  pinMode(dir1,OUTPUT);
  pinMode(dir2,OUTPUT);
  digitalWrite(dir1,HIGH);
  digitalWrite(dir2,LOW);
}

void setupColor (){
  pinMode(S0, OUTPUT); 
  pinMode(S1, OUTPUT); 
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT); 
  pinMode(sensorOut, INPUT); 
  digitalWrite(S0,HIGH); 
  digitalWrite(S1,LOW); 
} 

void loop(){

  loopTurntable();
  loopColor();
}

void loopTurntable(){
  
  if (IR.decode(&cmd) == 0) return;{
}

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 loopColor(){
  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); Serial.print("");
  
if mSpeed==0;
redF = pulseIn(sensorOut,0); 
  redC = map(redF, 0,0, 0,0);

greenF = pulseIn(sensorOut,0); 
  greenC = map(redF, 0,0, 0,0);

blueF = pulseIn(sensorOut,0); 
  blueC = map(redF, 0,0, 0,0);

  
}

Don't you do the IR.resume after you've examined the result?

...is just about the most irritating, pointless phrase you could post on this forum

It's hard to get those to work indeed...
cod

:slight_smile:

joke aside, what's the analogWrite(speedPin,mSpeed); doing outside the if blocks?

The IR library is undergoing some changes, you might want to adjust your code to the new API

PS= what's the empty statement doing { } here

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

Sorry. I don't I did the IR.resume. What is the IR.resume for? Could you please let me know how to do it?

Didn't the compiler barf on that?

Haha, I didn't realize I typed 'cod' instead of 'code'.

This is for letting the motor work.

This is for getting the data from the sensor. I want to keep getting RGB value after click the start button. If the code like this (if (IR.decode(&cmd) == 0)), I need to keep clicking button to get RGB value from the sensor.