awinograd

Lab 5 - Datalogger -> SD

May 4th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.45 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 <SD.h>
  25. const int chipSelect = 12;
  26.  
  27. #include <EEPROM.h>
  28.  
  29. // the current address in the EEPROM (i.e. which byte
  30. // we're going to write to next)
  31. int addrRed = 0;
  32. int addrGreen = 1;
  33. int addrBlue = 2;
  34. int addrActive = 3;
  35.  
  36. File dataFile;
  37.  
  38.  
  39. void initSD(){
  40.   Serial.begin(9600);
  41.   Serial.print("Initializing SD card...");
  42.   // see if the card is present and can be initialized:
  43.   if (!SD.begin(chipSelect)) {
  44.     Serial.println("Card failed, or not present");
  45.     // don't do anything more:
  46.     return;
  47.   }
  48.   Serial.println("card initialized.");  
  49. }
  50.  
  51. void setup()
  52. {
  53.   initSD();
  54.   lcd.begin(16, 2);
  55.   /* Setup encoder pins as inputs */
  56.   pinMode(ENC_A, INPUT_PULLUP);
  57.   pinMode(ENC_B, INPUT_PULLUP);
  58.   pinMode(RED, OUTPUT);
  59.   pinMode(GREEN, OUTPUT);
  60.   pinMode(BLUE, OUTPUT);
  61.   pinMode(BUTTON, INPUT);
  62.   makeWhite();
  63.   red = EEPROM.read(addrRed);
  64.   green = EEPROM.read(addrGreen);
  65.   blue = EEPROM.read(addrBlue);
  66.   activeColor = EEPROM.read(addrActive);
  67.   setRed();
  68.   setGreen();
  69.   setBlue();  
  70.   updateDisplay();
  71.   Serial.begin (9600);
  72.   Serial.println("Start");
  73. }
  74.  
  75. int updateColor(int current, int delta){
  76.   int temp = current+delta;
  77.   if(temp > 255) return 255;
  78.   if(temp < 0) return 0;
  79.   return temp;  
  80. }
  81.  
  82. void loop()
  83. {
  84.   static unsigned long counter4x = 0;      //the SparkFun encoders jump by 4 states from detent to detent
  85.   static unsigned long counter = 0;
  86.   static unsigned long prevCounter = 0;
  87.  
  88.   int tmpdata;
  89.  
  90.   if (digitalRead(BUTTON))
  91.   {
  92.     activeColor = (activeColor+1)%3;
  93.     EEPROM.write(addrActive, activeColor);
  94.     Serial.println("activeColor: " + (String)activeColor);
  95.     updateDisplay();
  96.     delay(250);
  97.   }
  98.  
  99.   tmpdata = read_encoder();
  100.   if( tmpdata ) {  
  101.     if (digitalRead(PUSHBTN)){
  102.       tmpdata *= 5;
  103.     }
  104.     counter4x += tmpdata;
  105.     counter = counter4x/4;
  106.     if (prevCounter != counter){
  107.       Serial.print("Counter value: ");
  108.       Serial.println(counter);
  109.  
  110.       switch(activeColor){
  111.        case 0:
  112.                red = updateColor(red, tmpdata);
  113.                setRed();
  114.                break;
  115.        case 1:
  116.                green = updateColor(green, tmpdata);
  117.                setGreen();
  118.                break;
  119.        case 2:
  120.                blue = updateColor(blue, tmpdata);  
  121.                setBlue();
  122.                break;
  123.        default:
  124.         break;
  125.        
  126.       }
  127.       /* Light up the LEDs with all possible color combinations - raster scan
  128.       analogWrite(RED, counter % 255);
  129.       analogWrite(GREEN, (counter/255) % 255);
  130.       analogWrite(BLUE, (counter/255/255) % 255);*/
  131.     }
  132.     updateDisplay();
  133.     prevCounter = counter;
  134.     dataFile = SD.open("datalog.txt", FILE_WRITE);  
  135.     dataFile.println("RGB=(" + (String)red + "," + (String)green + "," + (String)blue +")");    
  136.     dataFile.close();
  137.   }
  138. }
  139.  
  140. void setRed(){
  141.   EEPROM.write(addrRed, red);
  142.   analogWrite(RED, 255-red);
  143.   Serial.print("R: ");
  144.   Serial.println(red);
  145. }
  146.  
  147. void setGreen(){
  148.   EEPROM.write(addrGreen, green);              
  149.   analogWrite(GREEN, 255-green);
  150.   Serial.print("G: ");
  151.   Serial.println(green);
  152. }
  153.  
  154. void setBlue(){
  155.   EEPROM.write(addrBlue, blue);              
  156.   analogWrite(BLUE, 255-blue);
  157.   Serial.print("B: ");
  158.   Serial.println(blue);    
  159. }
  160.  
  161. /* returns change in encoder state (-1,0,1) */
  162. int read_encoder()
  163. {
  164.   static int enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  165.   static unsigned char old_AB = 0;
  166.   /**/
  167.   old_AB <<= 2;                   //remember previous state
  168.   old_AB |= ( ENC_PORT & 0x03 );  //add current state
  169.   return ( enc_states[( old_AB & 0x0f )]);
  170. }
  171.  
  172. void updateDisplay(){
  173.  lcd.clear();
  174.  lcd.setCursor(0,0);
  175.  lcd.print("RGB=(" + (String)red + "," + (String)green + "," + (String)blue +")");
  176.  lcd.setCursor(activeColor,1);
  177.  lcd.print("^");
  178. }
  179.  
  180. /* turn on all the LEDs, so it's white */
  181. void makeWhite()
  182. {
  183.   analogWrite(RED, 0);
  184.   analogWrite(GREEN, 0);
  185.   analogWrite(BLUE, 0);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment