Last active
September 5, 2022 13:25
-
-
Save Tech500/2b18ac2c5f90fa15c7a4541a9d55472b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Adafruit Seesaw library: "multiencoder.ino" example; expanded to use Sparkfun Alphanumeric I²C display. | |
| Modified by William Lucid | |
| "TP Sept4a_4results.ino" sketch filename --a work-in-progress | |
| */ | |
| #include <Wire.h> | |
| #include "Adafruit_seesaw.h" | |
| #include <seesaw_neopixel.h> | |
| #include <SparkFun_Alphanumeric_Display.h> | |
| HT16K33 display; | |
| #define SS_SWITCH 24 // this is the pin on the encoder connected to switch | |
| #define SS_NEOPIX 6 // this is the pin on the encoder connected to neopixel | |
| #define SEESAW_BASE_ADDR 0x36 // I2C address, starts with 0x36 | |
| #define DELAY_TIME 1000 //1 second delay | |
| unsigned long lastExecutedMillis = 0; // create a variable to save the last executed time | |
| int result, result1, result2, result3, result4; //ASCII Character code in decimal | |
| // create 4 encoders! | |
| Adafruit_seesaw encoders[4]; | |
| // create 4 encoder pixels | |
| seesaw_NeoPixel encoder_pixels[4] = { | |
| seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800), | |
| seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800), | |
| seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800), | |
| seesaw_NeoPixel(1, SS_NEOPIX, NEO_GRB + NEO_KHZ800)}; | |
| int32_t encoder_positions[] = {0, 0, 0, 0}; | |
| bool found_encoders[] = {false, false, false, false}; | |
| uint8_t enc; | |
| int32_t new_position; | |
| void setup() { | |
| Serial.begin(9600); | |
| // wait for serial port to open | |
| while (!Serial) delay(10); | |
| Serial.println("Alphanumeric display + seesaw Encoders test"); | |
| Wire.begin(21, 22); | |
| //check if display will acknowledge | |
| if (display.begin() == false) | |
| { | |
| Serial.println("Device did not acknowledge! Freezing."); | |
| while(1); | |
| } | |
| Serial.println("Display acknowledged."); | |
| Serial.println("Looking for seesaws!"); | |
| for (uint8_t enc=0; enc<sizeof(found_encoders); enc++) { | |
| // See if we can find encoders on this address | |
| if (! encoders[enc].begin(SEESAW_BASE_ADDR + enc) || | |
| ! encoder_pixels[enc].begin(SEESAW_BASE_ADDR + enc)) { | |
| Serial.print("Couldn't find encoder # "); | |
| Serial.println(enc); | |
| } else { | |
| Serial.print("Found encoder + pixel #"); | |
| Serial.println(enc); | |
| uint32_t version = ((encoders[enc].getVersion() >> 16) & 0xFFFF); | |
| if (version != 4991){ | |
| Serial.print("Wrong firmware loaded? "); | |
| Serial.println(version); | |
| while(1) delay(10); | |
| } | |
| Serial.println("Found Product 4991"); | |
| // use a pin for the built in encoder switch | |
| encoders[enc].pinMode(SS_SWITCH, INPUT_PULLUP); | |
| // get starting position | |
| encoder_positions[enc] = encoders[enc].getEncoderPosition(); | |
| Serial.println("Turning on interrupts"); | |
| delay(10); | |
| encoders[enc].setGPIOInterrupts((uint32_t)1 << SS_SWITCH, 1); | |
| encoders[enc].enableEncoderInterrupt(); | |
| // set not so bright! | |
| encoder_pixels[enc].setBrightness(30); | |
| encoder_pixels[enc].show(); | |
| found_encoders[enc] = true; | |
| } | |
| } | |
| Serial.println("Encoders started"); | |
| } | |
| void loop(){ | |
| unsigned long currentMillis = millis(); // copy the current millis to our variable | |
| if (currentMillis - lastExecutedMillis >= DELAY_TIME) // check to see how much time has passed | |
| { | |
| lastExecutedMillis = currentMillis; // save the last executed time | |
| } | |
| for (enc=0; enc<sizeof(found_encoders); enc++) { | |
| if (found_encoders[enc] == false) continue; | |
| new_position = encoders[enc].getEncoderPosition(); | |
| // did we move around? | |
| if (encoder_positions[enc] != new_position) { | |
| Serial.print("Encoder #"); | |
| Serial.print(enc); | |
| Serial.print(" -> "); | |
| if(enc == 0) | |
| { | |
| //------------------------------------------ | |
| // Code from paulfjujo, on rntlabs.com forum | |
| if (encoder_positions[enc]< 27) | |
| result1 = 65 + encoder_positions[enc]; // display character | |
| else | |
| result1 = 48 + encoder_positions[enc]-27; // display number | |
| encoder_positions[enc] = new_position; | |
| //------------------------------------------ | |
| } | |
| if(enc == 1) | |
| { | |
| //------------------------------------------ | |
| // Code from paulfjujo, on rntlabs.com forum | |
| if (encoder_positions[enc]< 27) | |
| result2 = 65 + encoder_positions[enc]; // display character | |
| else | |
| result2 = 48 + encoder_positions[enc]-27; // display number | |
| encoder_positions[enc] = new_position; | |
| //------------------------------------------ | |
| } | |
| if(enc == 2) | |
| { | |
| //------------------------------------------ | |
| // Code from paulfjujo, on rntlabs.com forum | |
| if (encoder_positions[enc]< 27) | |
| result3 = 65 + encoder_positions[enc]; // display character | |
| else | |
| result3 = 48 + encoder_positions[enc]-27; // display number | |
| encoder_positions[enc] = new_position; | |
| //------------------------------------------ | |
| } | |
| if(enc == 3) | |
| { | |
| //------------------------------------------ | |
| // Code from paulfjujo, on rntlabs.com forum | |
| if (encoder_positions[enc]< 27) | |
| result4 = 65 + encoder_positions[enc]; // display character | |
| else | |
| result4 = 48 + encoder_positions[enc]-27; // display number | |
| encoder_positions[enc] = new_position; | |
| //------------------------------------------ | |
| } | |
| Serial.println(new_position); | |
| Serial.println(""); | |
| Serial.write(result); //ASCII Character | |
| Serial.println(""); | |
| // change the neopixel color, mulitply the new positiion by 4 to speed it up | |
| encoder_pixels[enc].setPixelColor(0, Wheel((new_position*4) & 0xFF)); | |
| encoder_pixels[enc].show(); | |
| } | |
| if (! encoders[enc].digitalRead(SS_SWITCH)) { | |
| Serial.print("Encoder #"); | |
| Serial.print(enc); | |
| Serial.println(" pressed"); | |
| } | |
| } | |
| display_one(); //Display on alphanumeric display | |
| yield(); | |
| delay(10); | |
| } | |
| void display_one(){ | |
| //The input to setBrightness is a duty cycle over 16 | |
| //So, acceptable inputs to this function are ints between 0 (1/16 brightness) and 15 (full brightness) | |
| display.setBrightness(11); | |
| display.printChar(char(result1), 0); // when enc = 0 and new_position = 1; result1 displays A | |
| display.printChar(char(result2), 1); | |
| display.printChar(char(result3), 2); | |
| display.printChar(char(result4), 3); | |
| display.updateDisplay(); | |
| } | |
| uint32_t Wheel(byte WheelPos) { | |
| WheelPos = 255 - WheelPos; | |
| if (WheelPos < 85) { | |
| return seesaw_NeoPixel::Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
| } | |
| if (WheelPos < 170) { | |
| WheelPos -= 85; | |
| return seesaw_NeoPixel::Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
| } | |
| WheelPos -= 170; | |
| return seesaw_NeoPixel::Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment