Advertisement
Guest User

Untitled

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