Advertisement
myarkqub

Sketch_sep11_1

Sep 21st, 2018
211
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. void setup() {
  19.   pinMode(S0, OUTPUT);
  20.   pinMode(S1, OUTPUT);
  21.   pinMode(S2, OUTPUT);
  22.   pinMode(S3, OUTPUT);
  23.   pinMode(sensorOut, INPUT);
  24.  
  25.   servo_red.attach(9);
  26.   servo_green.attach(10);
  27.   servo_blue.attach(11);
  28.  
  29.   servo_red.write(0);
  30.   servo_green.write(0);
  31.   servo_blue.write(0);
  32.  
  33.   // Setting frequency-scaling to 20%
  34.   digitalWrite(S0,HIGH);
  35.   digitalWrite(S1,LOW);
  36.  
  37.   Serial.begin(9600);
  38. }
  39. void loop() {
  40.  
  41.   // Setting red filtered photodiodes to be read
  42.   digitalWrite(S2,LOW);
  43.   digitalWrite(S3,LOW);
  44.   // Reading the output frequency
  45.   frequency = pulseIn(sensorOut, LOW);
  46.   //Remaping the value of the frequency to the RGB Model of 0 to 255
  47.   frequency = map(frequency, 25,72,255,0);
  48.   // Printing the value on the serial monitor
  49.   Serial.print("R= ");//printing name
  50.   Serial.print(frequency);//printing RED color frequency
  51.   Serial.print("  ");
  52.   if(frequency <= 100){
  53.     Serial.print("Red frequency < 100:");
  54.     Serial.println(frequency);
  55.     for (pos_red = 0; pos_red <= 180; pos_red += 1) { // goes from 0 degrees to 180 degrees
  56.       // in steps of 1 degree
  57.       servo_red.write(pos_red);              // tell servo to go to position in variable 'pos'
  58.       delay(15);                       // waits 15ms for the servo to reach the position
  59.     }
  60.     for (pos_red = 180; pos_red >= 0; pos_red -= 1) { // goes from 180 degrees to 0 degrees
  61.       servo_red.write(pos_red);              // tell servo to go to position in variable 'pos'
  62.       delay(15);                       // waits 15ms for the servo to reach the position
  63.     }
  64.   }
  65.   delay(100);
  66.   // Setting Green filtered photodiodes to be read
  67.   digitalWrite(S2,HIGH);
  68.   digitalWrite(S3,HIGH);
  69.   // Reading the output frequency
  70.   frequency = pulseIn(sensorOut, LOW);
  71.   //Remaping the value of the frequency to the RGB Model of 0 to 255
  72.   frequency = map(frequency, 30,90,255,0);
  73.   // Printing the value on the serial monitor
  74.   Serial.print("G= ");//printing name
  75.   Serial.print(frequency);//printing RED color frequency
  76.   Serial.print("  ");
  77.   if(frequency <= 100){
  78.     Serial.print("Green frequency < 100:");
  79.     Serial.println(frequency);
  80.     for (pos_green = 0; pos_green <= 180; pos_green += 1) { // goes from 0 degrees to 180 degrees
  81.       // in steps of 1 degree
  82.       servo_green.write(pos_green);              // tell servo to go to position in variable 'pos'
  83.       delay(15);                       // waits 15ms for the servo to reach the position
  84.     }
  85.     for (pos_green = 180; pos_green >= 0; pos_green -= 1) { // goes from 180 degrees to 0 degrees
  86.       servo_green.write(pos_green);              // tell servo to go to position in variable 'pos'
  87.       delay(15);                       // waits 15ms for the servo to reach the position
  88.     }
  89.   }
  90.   delay(100);
  91.   // Setting Blue filtered photodiodes to be read
  92.   digitalWrite(S2,LOW);
  93.   digitalWrite(S3,HIGH);
  94.   // Reading the output frequency
  95.   frequency = pulseIn(sensorOut, LOW);
  96.   //Remaping the value of the frequency to the RGB Model of 0 to 255
  97.   frequency = map(frequency, 25,70,255,0);
  98.   // Printing the value on the serial monitor
  99.   Serial.print("B= ");//printing name
  100.   Serial.print(frequency);//printing RED color frequency
  101.   Serial.println("  ");
  102.   if(frequency <= 100){
  103.     Serial.print("Blue frequency < 100:");
  104.     Serial.println(frequency);
  105.     for (pos_blue = 0; pos_blue <= 180; pos_blue += 1) { // goes from 0 degrees to 180 degrees
  106.       // in steps of 1 degree
  107.       servo_blue.write(pos_blue);              // tell servo to go to position in variable 'pos'
  108.       delay(15);                       // waits 15ms for the servo to reach the position
  109.     }
  110.     for (pos_blue = 180; pos_blue >= 0; pos_blue -= 1) { // goes from 180 degrees to 0 degrees
  111.       servo_blue.write(pos_blue);              // tell servo to go to position in variable 'pos'
  112.       delay(15);                       // waits 15ms for the servo to reach the position
  113.     }
  114.   }
  115.   delay(100);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement