awinograd

RGB Changer

Apr 30th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. /* Rotary encoder test code
  2.  * Modified from http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino
  3.  * by Oleg
  4.  */
  5.  // include the library code:
  6. #include <LiquidCrystal.h>
  7. // initialize the library with the numbers of the interface pins
  8. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  9.  
  10. #define ENC_A 0
  11. #define ENC_B 1
  12. #define RED 10
  13. #define GREEN 9
  14. #define BLUE 14
  15. #define BUTTON A8
  16. #define PUSHBTN 8
  17. #define ENC_PORT PINB
  18.  
  19. int activeColor = 0; //R,G,B
  20. int red = 0;
  21. int green = 0;
  22. int blue = 0;
  23.  
  24. #include <EEPROM.h>
  25. // the current address in the EEPROM (i.e. which byte
  26. // we're going to write to next)
  27. int addrRed = 0;
  28. int addrGreen = 1;
  29. int addrBlue = 2;
  30. int addrActive = 3;
  31.  
  32. void setup()
  33. {
  34.   lcd.begin(16, 2);
  35.   /* Setup encoder pins as inputs */
  36.   pinMode(ENC_A, INPUT_PULLUP);
  37.   pinMode(ENC_B, INPUT_PULLUP);
  38.   pinMode(RED, OUTPUT);
  39.   pinMode(GREEN, OUTPUT);
  40.   pinMode(BLUE, OUTPUT);
  41.   pinMode(BUTTON, INPUT);
  42.   makeWhite();
  43.   red = EEPROM.read(addrRed);
  44.   green = EEPROM.read(addrGreen);
  45.   blue = EEPROM.read(addrBlue);
  46.   activeColor = EEPROM.read(addrActive);
  47.   setRed();
  48.   setGreen();
  49.   setBlue();  
  50.   updateDisplay();
  51.   Serial.begin (9600);
  52.   Serial.println("Start");
  53. }
  54.  
  55. int updateColor(int current, int delta){
  56.   int temp = current+delta;
  57.   if(temp > 255) return 255;
  58.   if(temp < 0) return 0;
  59.   return temp;  
  60. }
  61.  
  62. void loop()
  63. {
  64.   static unsigned long counter4x = 0;      //the SparkFun encoders jump by 4 states from detent to detent
  65.   static unsigned long counter = 0;
  66.   static unsigned long prevCounter = 0;
  67.  
  68.   int tmpdata;
  69.  
  70.   if (digitalRead(BUTTON))
  71.   {
  72.     activeColor = (activeColor+1)%3;
  73.     EEPROM.write(addrActive, activeColor);
  74.     Serial.println("activeColor: " + (String)activeColor);
  75.     updateDisplay();
  76.     delay(250);
  77.   }
  78.  
  79.   tmpdata = read_encoder();
  80.   if( tmpdata ) {
  81.     if (digitalRead(PUSHBTN)){
  82.       tmpdata *= 5;
  83.     }
  84.     counter4x += tmpdata;
  85.     counter = counter4x/4;
  86.     if (prevCounter != counter){
  87.       Serial.print("Counter value: ");
  88.       Serial.println(counter);
  89.  
  90.       switch(activeColor){
  91.        case 0:
  92.                red = updateColor(red, tmpdata);
  93.                setRed();
  94.                break;
  95.        case 1:
  96.                green = updateColor(green, tmpdata);
  97.                setGreen();
  98.                break;
  99.        case 2:
  100.                blue = updateColor(blue, tmpdata);  
  101.                setBlue();
  102.                break;
  103.        default:
  104.         break;
  105.        
  106.       }
  107.       /* Light up the LEDs with all possible color combinations - raster scan
  108.       analogWrite(RED, counter % 255);
  109.       analogWrite(GREEN, (counter/255) % 255);
  110.       analogWrite(BLUE, (counter/255/255) % 255);*/
  111.     }
  112.     updateDisplay();
  113.     prevCounter = counter;
  114.   }
  115. }
  116.  
  117. void setRed(){
  118.   EEPROM.write(addrRed, red);
  119.   analogWrite(RED, 255-red);
  120.   Serial.print("R: ");
  121.   Serial.println(red);
  122. }
  123.  
  124. void setGreen(){
  125.   EEPROM.write(addrGreen, green);              
  126.   analogWrite(GREEN, 255-green);
  127.   Serial.print("G: ");
  128.   Serial.println(green);
  129. }
  130.  
  131. void setBlue(){
  132.   EEPROM.write(addrBlue, blue);              
  133.   analogWrite(BLUE, 255-blue);
  134.   Serial.print("B: ");
  135.   Serial.println(blue);    
  136. }
  137.  
  138. /* returns change in encoder state (-1,0,1) */
  139. int read_encoder()
  140. {
  141.   static int enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  142.   static unsigned char old_AB = 0;
  143.   /**/
  144.   old_AB <<= 2;                   //remember previous state
  145.   old_AB |= ( ENC_PORT & 0x03 );  //add current state
  146.   return ( enc_states[( old_AB & 0x0f )]);
  147. }
  148.  
  149. void updateDisplay(){
  150.  lcd.clear();
  151.  lcd.setCursor(0,0);
  152.  lcd.print("RGB=(" + (String)red + "," + (String)green + "," + (String)blue +")");
  153.  lcd.setCursor(activeColor,1);
  154.  lcd.print("^");
  155. }
  156.  
  157. /* turn on all the LEDs, so it's white */
  158. void makeWhite()
  159. {
  160.   analogWrite(RED, 0);
  161.   analogWrite(GREEN, 0);
  162.   analogWrite(BLUE, 0);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment