Advertisement
myarkqub

sketch_sep11c

Sep 24th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. #define S0 4
  4. #define S1 5
  5. #define S2 6
  6. #define S3 7
  7. #define sensorOut 8
  8.  
  9. Servo servo_red;
  10. Servo servo_green;
  11. Servo servo_blue;
  12.  
  13. int frequency = 0;
  14. int pos_red = 0;
  15. int pos_green = 0;
  16. int pos_blue = 0;
  17.  
  18. int led_red = 1;
  19. int led_green = 2;
  20. int led_blue = 3;
  21.  
  22. void setup() {
  23.   pinMode(S0, OUTPUT);
  24.   pinMode(S1, OUTPUT);
  25.   pinMode(S2, OUTPUT);
  26.   pinMode(S3, OUTPUT);
  27.   pinMode(sensorOut, INPUT);
  28.  
  29.   pinMode(led_red, OUTPUT);
  30.   pinMode(led_green, OUTPUT);
  31.   pinMode(led_blue, OUTPUT);
  32.  
  33.   servo_red.attach(9);
  34.   servo_green.attach(10);
  35.   servo_blue.attach(11);
  36.  
  37.   servo_red.write(0);
  38.   servo_green.write(0);
  39.   servo_blue.write(0);
  40.  
  41.   // Setting frequency-scaling to 20%
  42.   digitalWrite(S0,HIGH);
  43.   digitalWrite(S1,LOW);
  44.  
  45.   Serial.begin(9600);
  46. }
  47. void loop() {
  48.  
  49.   digitalWrite(led_red, LOW);
  50.   digitalWrite(led_green, LOW);
  51.   digitalWrite(led_blue, LOW);
  52.  
  53.   // Setting red filtered photodiodes to be read
  54.   digitalWrite(S2,LOW);
  55.   digitalWrite(S3,LOW);
  56.   // Reading the output frequency
  57.   frequency = pulseIn(sensorOut, LOW);
  58.   //Remaping the value of the frequency to the RGB Model of 0 to 255
  59.   frequency = map(frequency, 25,72,255,0);
  60.   // Printing the value on the serial monitor
  61.   Serial.print("R= ");//printing name
  62.   Serial.print(frequency);//printing RED color frequency
  63.   Serial.print("  ");
  64.   if(frequency >= 100){
  65.  
  66.     digitalWrite(led_red, HIGH);
  67.    
  68.     Serial.print("Red frequency < 100:");
  69.     Serial.println(frequency);
  70.     for (pos_red = 0; pos_red <= 180; pos_red += 1) { // goes from 0 degrees to 180 degrees
  71.       // in steps of 1 degree
  72.       servo_red.write(pos_red);              // tell servo to go to position in variable 'pos'
  73.       delay(15);                       // waits 15ms for the servo to reach the position
  74.     }
  75.     for (pos_red = 180; pos_red >= 0; pos_red -= 1) { // goes from 180 degrees to 0 degrees
  76.       servo_red.write(pos_red);              // tell servo to go to position in variable 'pos'
  77.       delay(15);                       // waits 15ms for the servo to reach the position
  78.     }
  79.   }
  80.   delay(100);
  81.   // Setting Green filtered photodiodes to be read
  82.   digitalWrite(S2,HIGH);
  83.   digitalWrite(S3,HIGH);
  84.   // Reading the output frequency
  85.   frequency = pulseIn(sensorOut, LOW);
  86.   //Remaping the value of the frequency to the RGB Model of 0 to 255
  87.   frequency = map(frequency, 30,90,255,0);
  88.   // Printing the value on the serial monitor
  89.   Serial.print("G= ");//printing name
  90.   Serial.print(frequency);//printing RED color frequency
  91.   Serial.print("  ");
  92.   if(frequency >= 100){
  93.  
  94.     digitalWrite(led_green, HIGH);
  95.    
  96.     Serial.print("Green frequency < 100:");
  97.     Serial.println(frequency);
  98.     for (pos_green = 0; pos_green <= 180; pos_green += 1) { // goes from 0 degrees to 180 degrees
  99.       // in steps of 1 degree
  100.       servo_green.write(pos_green);              // tell servo to go to position in variable 'pos'
  101.       delay(15);                       // waits 15ms for the servo to reach the position
  102.     }
  103.     for (pos_green = 180; pos_green >= 0; pos_green -= 1) { // goes from 180 degrees to 0 degrees
  104.       servo_green.write(pos_green);              // tell servo to go to position in variable 'pos'
  105.       delay(15);                       // waits 15ms for the servo to reach the position
  106.     }
  107.   }
  108.   delay(100);
  109.   // Setting Blue filtered photodiodes to be read
  110.   digitalWrite(S2,LOW);
  111.   digitalWrite(S3,HIGH);
  112.   // Reading the output frequency
  113.   frequency = pulseIn(sensorOut, LOW);
  114.   //Remaping the value of the frequency to the RGB Model of 0 to 255
  115.   frequency = map(frequency, 25,70,255,0);
  116.   // Printing the value on the serial monitor
  117.   Serial.print("B= ");//printing name
  118.   Serial.print(frequency);//printing RED color frequency
  119.   Serial.println("  ");
  120.   if(frequency >= 100){
  121.  
  122.     digitalWrite(led_blue, HIGH);
  123.    
  124.     Serial.print("Blue frequency < 100:");
  125.     Serial.println(frequency);
  126.     for (pos_blue = 0; pos_blue <= 180; pos_blue += 1) { // goes from 0 degrees to 180 degrees
  127.       // in steps of 1 degree
  128.       servo_blue.write(pos_blue);              // tell servo to go to position in variable 'pos'
  129.       delay(15);                       // waits 15ms for the servo to reach the position
  130.     }
  131.     for (pos_blue = 180; pos_blue >= 0; pos_blue -= 1) { // goes from 180 degrees to 0 degrees
  132.       servo_blue.write(pos_blue);              // tell servo to go to position in variable 'pos'
  133.       delay(15);                       // waits 15ms for the servo to reach the position
  134.     }
  135.   }
  136.   delay(100);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement