1

I am using a DIY colour sensor (RGB LED and LDR) and a servo to make a skittles sorter. I am testing out just the colour detection now and printing the RGB values to serial. It worked fine until I added servo code. Now it stops printing to serial after about 4 cycles. Not sure why this is happening. I have tried using other programs besides using the Arduino IDE but the same issue.

I am using a NodeMCU with Arduino code:

#define S0 4
#define S1 0
#define S2 2
#include <Servo.h> 
Servo topServo;
int frequency = 0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  topServo.attach(14);
 Serial.begin(9600);
 Serial.print("Start");
}

void loop() {
  topServo.write(125);
  delay(500);

  for(int i = 150; i > 80; i--) {
    topServo.write(i);
    delay(5);
  }
  checkcolours();
  delay(500); 
}
void checkcolours(){
 //RED
digitalWrite(S1,LOW);
digitalWrite(S2,LOW);
digitalWrite(S0,HIGH);
frequency = analogRead(17);
int R = frequency;
Serial.print("R= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print("  ");
delay(100);

//GREEN
digitalWrite(S0,LOW);
digitalWrite(S2,LOW);
digitalWrite(S1,HIGH);
frequency = analogRead(17);
int G = frequency;
Serial.print("G= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print("  ");
delay(100);

//BLUE
digitalWrite(S0,LOW);
digitalWrite(S1,LOW);
digitalWrite(S2,HIGH);
frequency = analogRead(17);
int B = frequency;
Serial.print("B= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.println("  ");
delay(100);
returnservo();  
  }
void returnservo(){
  topServo.write(30);
}
  • Are you sure you want to use pin 0 for S1? it seems that is the RX from the serial – Michel Keijzers Jun 29 '17 at 9:34
  • Oh I didn't know that. It is actually pin D2 on the NodeMCU which maps to pin 0 for the Arduino IDE, could this be the issue? – Bruno Jun 29 '17 at 9:38
  • I'm not sure, but I think it's very easy to change the number and change the wire for a small test – Michel Keijzers Jun 29 '17 at 9:50
  • Great, ill check and let you know! Thank you so much :) – Bruno Jun 29 '17 at 10:01
  • (I don't promise anything, just an idea, otherwise I would put it as answer) – Michel Keijzers Jun 29 '17 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.