Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1.  
  2. #define SERIAL // Comment this line out, to disable serial comms
  3.  
  4.  
  5. #define greenLEDPin 9 // LED connected to digital pin 9
  6. #define redLEDPin 10 // LED connected to digital pin 10
  7. #define blueLEDPin 11 // LED connected to digital pin 11
  8.  
  9. #define redSensorPin A0 // pin with the photoresistor with the red gel
  10. #define greenSensorPin A1 // pin with the photoresistor with the green gel
  11. #define blueSensorPin A2 // pin with the photoresistor with the blue gel
  12.  
  13. int redValue = 0; // value to write to the red LED
  14. int greenValue = 0; // value to write to the green LED
  15. int blueValue = 0; // value to write to the blue LED
  16.  
  17. int redSensorValue = 0; // variable to hold the value from the red sensor
  18. int greenSensorValue = 0; // variable to hold the value from the green sensor
  19. int blueSensorValue = 0; // variable to hold the value from the blue sensor
  20.  
  21. void setup() {
  22.  
  23. #ifdef SERIAL
  24. Serial.begin(9600); // initialize serial communications at 9600 bps:
  25. #endif
  26.  
  27.  
  28. // set the digital pins as outputs
  29. pinMode(greenLEDPin, OUTPUT);
  30. pinMode(redLEDPin, OUTPUT);
  31. pinMode(blueLEDPin, OUTPUT);
  32. }
  33.  
  34. void loop() {
  35. // Read the sensors first:
  36. // read the value from the red-filtered photoresistor:
  37. redSensorValue = analogRead(redSensorPin);
  38. // give the ADC a moment to settle
  39. delay(5);
  40.  
  41.  
  42. // read the value from the green-filtered photoresistor:
  43. greenSensorValue = analogRead(greenSensorPin);
  44. // give the ADC a moment to settle
  45. delay(5);
  46.  
  47.  
  48. // read the value from the blue-filtered photoresistor:
  49. blueSensorValue = analogRead(blueSensorPin);
  50.  
  51. #ifdef SERIAL
  52. // print out the values to the Serial Monitor
  53. Serial.print("raw sensor Values \t red: ");
  54. Serial.print(redSensorValue);
  55. Serial.print("\t green: ");
  56. Serial.print(greenSensorValue);
  57. Serial.print("\t Blue: ");
  58. Serial.println(blueSensorValue);
  59. #endif
  60.  
  61. /*
  62. In order to use the values from the sensor for the LED, you need to do some
  63. math. The ADC provides a 10-bit number, but analogWrite() uses 8 bits.
  64. You'll want to divide your sensor readings by 4 to keep them in range
  65. of the output.
  66. */
  67. redValue = redSensorValue / 4;
  68. greenValue = greenSensorValue / 4;
  69. blueValue = blueSensorValue / 4;
  70.  
  71.  
  72. #ifdef SERIAL
  73. // print out the mapped values
  74. Serial.print("Mapped sensor Values \t red: ");
  75. Serial.print(redValue);
  76. Serial.print("\t green: ");
  77. Serial.print(greenValue);
  78. Serial.print("\t Blue: ");
  79. Serial.println(blueValue);
  80. #endif
  81.  
  82.  
  83. /*
  84. Now that you have a usable value, it's time to PWM the LED.
  85. */
  86. analogWrite(redLEDPin, redValue);
  87. analogWrite(greenLEDPin, greenValue);
  88. analogWrite(blueLEDPin, blueValue);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement