Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define RedLED 2
- #define GreenLED 3
- #define S0 4
- #define S1 5
- #define S2 7
- #define S3 6
- #define sensorOut 8
- int frequencyR = 0;
- int frequencyG = 0;
- int frequencyB = 0;
- int currentColor = 0;
- /////////////////////////////
- //Color is RED
- int Red[3] = { 27, 100, 80};
- //Color is Green
- int Green[3] = { 92, 60, 80};
- //Color is Yellow
- int Yellow[3] = { 20, 32, 53};
- /////////////////////////////
- //Color is White
- int White[3] = { 19, 21, 16};
- //Color is Black
- int Black[3] = {155, 160, 118};
- //Color is Blue
- int Blue[3] = {120, 50, 33};
- /////////////////////////////
- void setup() {
- pinMode(RedLED, OUTPUT);
- pinMode(GreenLED, OUTPUT);
- pinMode(S0, OUTPUT);
- pinMode(S1, OUTPUT);
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- pinMode(sensorOut, INPUT);
- digitalWrite(S0, HIGH);
- digitalWrite(S1, LOW);
- digitalWrite(RedLED, LOW);
- digitalWrite(GreenLED, LOW);
- Serial.begin(9600);
- }
- void loop() {
- TurnOnRedLED(2000);
- TurnOnGreenLED(2000);
- ReadColorSensor();
- SerialOutColor(frequencyR, frequencyG, frequencyB);
- }
- void ReadColorSensor() {
- digitalWrite(S2, LOW);
- digitalWrite(S3, LOW);
- frequencyR = pulseIn(sensorOut, LOW);
- Serial.print("R= ");
- Serial.print(frequencyR);
- Serial.print(" ");
- delay(100);
- digitalWrite(S2, HIGH);
- digitalWrite(S3, HIGH);
- frequencyG = pulseIn(sensorOut, LOW);
- Serial.print("G= ");
- Serial.print(frequencyG);
- Serial.print(" ");
- delay(100);
- digitalWrite(S2, LOW);
- digitalWrite(S3, HIGH);
- frequencyB = pulseIn(sensorOut, LOW);
- Serial.print("B= ");
- Serial.print(frequencyB);
- Serial.println(" ");
- delay(100);
- }
- void SerialOutColor(int a, int b, int c) {
- //Color is RED
- if (a > (Red[0] - 10) && a < (Red[0] + 10) && b > (Red[1] - 10) && b < (Red[1] + 10) && c > (Red[2] - 10) && c < (Red[2] + 10)) {
- currentColor = 1;
- Serial.println("Color is RED");
- delay(2000);
- }
- //Color is Green
- else if (a > (Green[0] - 10) && a < (Green[0] + 10) && b > (Green[1] - 10) && b < (Green[1] + 10) && c > (Green[2] - 10) && c < (Green[2] + 10)) {
- currentColor = 1;
- Serial.println("Color is Green");
- delay(2000);
- }
- //Color is Yellow
- else if (a > (Yellow[0] - 8) && a < (Yellow[0] + 8) && b > (Yellow[1] - 8) && b < (Yellow[1] + 8) && c > (Yellow[2] - 8) && c < (Yellow[2] + 8)) {
- currentColor = 1;
- Serial.println("Color is Yellow");
- delay(2000);
- }
- //Color is White
- else if (a > (White[0] - 8) && a < (White[0] + 8) && b > (White[1] - 8) && b < (White[1] + 8) && c > (White[2] - 8) && c < (White[2] + 8)) {
- currentColor = 2;
- Serial.println("Color is White");
- delay(2000);
- }
- //Color is Black
- else if (a > (Black[0] - 10) && a < (Black[0] + 10) && b > (Black[1] - 10) && b < (Black[1] + 10) && c > (Black[2] - 10) && c < (Black[2] + 10)) {
- currentColor = 2;
- Serial.println("Color is Black");
- delay(2000);
- }
- //Color is Blue
- else if (a > (Blue[0] - 10) && a < (Blue[0] + 10) && b > (Blue[1] - 10) && b < (Blue[1] + 10) && c > (Blue[2] - 10) && c < (Blue[2] + 10)) {
- currentColor = 2;
- Serial.println("Color is Blue");
- delay(2000);
- }
- }
- void TurnOnRedLED(int Time) {
- if (currentColor == 1) {
- digitalWrite(RedLED, HIGH);
- delay(Time);
- digitalWrite(RedLED, LOW);
- currentColor = 0;
- }
- }
- void TurnOnGreenLED(int Time) {
- if (currentColor == 2) {
- digitalWrite(GreenLED, HIGH);
- delay(Time);
- digitalWrite(GreenLED, LOW);
- currentColor = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment