Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Rotary encoder test code
- * Modified from http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino
- * by Oleg
- */
- // include the library code:
- #include <LiquidCrystal.h>
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- #define ENC_A 0
- #define ENC_B 1
- #define RED 10
- #define GREEN 9
- #define BLUE 14
- #define BUTTON A8
- #define PUSHBTN 8
- #define ENC_PORT PINB
- int activeColor = 0; //R,G,B
- int red = 0;
- int green = 0;
- int blue = 0;
- #include <SD.h>
- const int chipSelect = 12;
- #include <EEPROM.h>
- // the current address in the EEPROM (i.e. which byte
- // we're going to write to next)
- int addrRed = 0;
- int addrGreen = 1;
- int addrBlue = 2;
- int addrActive = 3;
- File dataFile;
- void initSD(){
- Serial.begin(9600);
- Serial.print("Initializing SD card...");
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- Serial.println("Card failed, or not present");
- // don't do anything more:
- return;
- }
- Serial.println("card initialized.");
- }
- void setup()
- {
- initSD();
- lcd.begin(16, 2);
- /* Setup encoder pins as inputs */
- pinMode(ENC_A, INPUT_PULLUP);
- pinMode(ENC_B, INPUT_PULLUP);
- pinMode(RED, OUTPUT);
- pinMode(GREEN, OUTPUT);
- pinMode(BLUE, OUTPUT);
- pinMode(BUTTON, INPUT);
- makeWhite();
- red = EEPROM.read(addrRed);
- green = EEPROM.read(addrGreen);
- blue = EEPROM.read(addrBlue);
- activeColor = EEPROM.read(addrActive);
- setRed();
- setGreen();
- setBlue();
- updateDisplay();
- Serial.begin (9600);
- Serial.println("Start");
- }
- int updateColor(int current, int delta){
- int temp = current+delta;
- if(temp > 255) return 255;
- if(temp < 0) return 0;
- return temp;
- }
- void loop()
- {
- static unsigned long counter4x = 0; //the SparkFun encoders jump by 4 states from detent to detent
- static unsigned long counter = 0;
- static unsigned long prevCounter = 0;
- int tmpdata;
- if (digitalRead(BUTTON))
- {
- activeColor = (activeColor+1)%3;
- EEPROM.write(addrActive, activeColor);
- Serial.println("activeColor: " + (String)activeColor);
- updateDisplay();
- delay(250);
- }
- tmpdata = read_encoder();
- if( tmpdata ) {
- if (digitalRead(PUSHBTN)){
- tmpdata *= 5;
- }
- counter4x += tmpdata;
- counter = counter4x/4;
- if (prevCounter != counter){
- Serial.print("Counter value: ");
- Serial.println(counter);
- switch(activeColor){
- case 0:
- red = updateColor(red, tmpdata);
- setRed();
- break;
- case 1:
- green = updateColor(green, tmpdata);
- setGreen();
- break;
- case 2:
- blue = updateColor(blue, tmpdata);
- setBlue();
- break;
- default:
- break;
- }
- /* Light up the LEDs with all possible color combinations - raster scan
- analogWrite(RED, counter % 255);
- analogWrite(GREEN, (counter/255) % 255);
- analogWrite(BLUE, (counter/255/255) % 255);*/
- }
- updateDisplay();
- prevCounter = counter;
- dataFile = SD.open("datalog.txt", FILE_WRITE);
- dataFile.println("RGB=(" + (String)red + "," + (String)green + "," + (String)blue +")");
- dataFile.close();
- }
- }
- void setRed(){
- EEPROM.write(addrRed, red);
- analogWrite(RED, 255-red);
- Serial.print("R: ");
- Serial.println(red);
- }
- void setGreen(){
- EEPROM.write(addrGreen, green);
- analogWrite(GREEN, 255-green);
- Serial.print("G: ");
- Serial.println(green);
- }
- void setBlue(){
- EEPROM.write(addrBlue, blue);
- analogWrite(BLUE, 255-blue);
- Serial.print("B: ");
- Serial.println(blue);
- }
- /* returns change in encoder state (-1,0,1) */
- int read_encoder()
- {
- static int enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
- static unsigned char old_AB = 0;
- /**/
- old_AB <<= 2; //remember previous state
- old_AB |= ( ENC_PORT & 0x03 ); //add current state
- return ( enc_states[( old_AB & 0x0f )]);
- }
- void updateDisplay(){
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("RGB=(" + (String)red + "," + (String)green + "," + (String)blue +")");
- lcd.setCursor(activeColor,1);
- lcd.print("^");
- }
- /* turn on all the LEDs, so it's white */
- void makeWhite()
- {
- analogWrite(RED, 0);
- analogWrite(GREEN, 0);
- analogWrite(BLUE, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment