Advertisement
Guest User

Untitled

a guest
May 24th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. /*
  2. Analog input, analog output, serial output
  3.  
  4. Reads an analog input pin, maps the result to a range from 0 to 255
  5. and uses the result to set the pulsewidth modulation (PWM) of an output pin.
  6. Also prints the results to the serial monitor.
  7.  
  8. The circuit:
  9. * potentiometer connected to analog pin 0.
  10. Center pin of the potentiometer goes to the analog pin.
  11. side pins of the potentiometer go to +5V and ground
  12. * LED connected from digital pin 9 to ground
  13.  
  14. created 29 Dec. 2008
  15. modified 9 Apr 2012
  16. by Tom Igoe
  17.  
  18. This example code is in the public domain.
  19.  
  20. */
  21.  
  22. // These constants won't change. They're used to give names
  23. // to the pins used:
  24.  
  25. const int analogInPinRGBLED_RED = A0; // Analog input pin that the potentiometer is attached to
  26. const int analogInPinRGBLED_BLUE = A1;
  27. const int analogInPinRGBLED_GREEN = A2;
  28.  
  29. const int analogOutPin = 9; // Analog output pin that the LED is attached to
  30.  
  31. int potRGBLED_RED = 0; // value read from the pot
  32. int potRGBLED_BLUE = 0;
  33. int potRGBLED_GREEN = 0;
  34.  
  35.  
  36. void setup() {
  37.  
  38. pinMode(RGBLED_RED, OUTPUT);
  39. pinMode(RGBLED_BLUE, OUTPUT);
  40. pinMode(RGBLED_GREEN,OUTPUT);
  41.  
  42. // initialize serial communications at 9600 bps:
  43. Serial.begin(9600);
  44. }
  45.  
  46. void loop() {
  47. // read the analog in value:
  48. potRGBLED_RED = analogRead(analogInPinRGBLED_RED);
  49. potRGBLED_GREEN = analogRead(analogInPinRGBLED_GREEN);
  50. potRGBLED_BLUE = analogRead(analogInPinRGBLED_BLUE);
  51.  
  52. // map it to the range of the analog out:
  53. potRGBLED_RED = map(potRGBLED_RED , 0, 1023, 0, 255);
  54. potRGBLED_GREEN = map(potRGBLED_GREEN , 0, 1023, 0, 255);
  55. potRGBLED_BLUE = map(potRGBLED_BLUE , 0, 1023, 0, 255);
  56.  
  57. // change the analog out value:
  58. analogWrite(RGBLED_RED, potRGBLED_RED);
  59. analogWrite(RGBLED_GREEN, potRGBLED_GREEN);
  60. analogWrite(RGBLED_BLUE, potRGBLED_BLUE);
  61.  
  62. // print the results to the serial monitor:
  63. Serial.print(potRGBLED_RED, HEX);
  64. Serial.print(potRGBLED_GREEN, HEX);
  65. Serial.println(potRGBLED_BLUE, HEX);
  66.  
  67. // wait 2 milliseconds before the next loop
  68. // for the analog-to-digital converter to settle
  69. // after the last reading:
  70. delay(2);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement