Hello,
I have a color sensor that reads two colored balls and puts them in their designated container with the help of a servo motor. I want to add another servo, independent of the color sensor, that starts after a certain time interval. I want that servo to initially begin at 90 degrees, and then go to 0 degrees after 5 seconds have passed and then to 180 degrees after that. I was looking into the millis command, but I'm not sure where to start.
Thanks
#include <Servo.h>
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
Servo motorServo;
Servo switchServo;
int frequency = 0;
int color=0;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 100%
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
motorServo.attach(7);
switchServo.attach(10);
Serial.begin(9600);
}
void loop() {
color = readColor();
delay(10);
switch (color) {
case 1:
motorServo.write(0);
delay(1000);
motorServo.write(90);
break;
case 2:
motorServo.write(180);
delay(1000);
motorServo.write(90);
break;
case 0:
break;
}
color = 0;
}
// Custom Function - readColor()
int readColor() {
// Setting red filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int R = frequency;
// Printing the value on the serial monitor
Serial.print("R= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Green filtered photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int G = frequency;
// Printing the value on the serial monitor
Serial.print("G= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.print(" ");
delay(50);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Reading the output frequency
frequency = pulseIn(sensorOut, LOW);
int B = frequency;
// Printing the value on the serial monitor
Serial.print("B= ");//printing name
Serial.print(frequency);//printing RED color frequency
Serial.println(" ");
delay(50);
if(R<110 & R>90 & G<105 & G>85){
color = 1; // Red
}
if (G<16 & G>6 & B<15 & B>7){
color = 2; // Blue
}
return color;
}