/Seesaw_library_with_switch.ino Secret
Last active
August 26, 2022 06:45
This file contains 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
// From Adafruit Seesaw librry example: | |
// https://github.com/adafruit/Adafruit_Seesaw/blob/master/examples/encoder/multiple_encoders/multiple_encoders.ino | |
// Modified by William Lucid, ab9nq@gmail.com Coding-is-a-work-in-progress. Compiler error below code. | |
#include "Arduino.h" | |
#include "Adafruit_seesaw.h" | |
#include <seesaw_neopixel.h> | |
#include "Adafruit_GFX.h" | |
#include "Adafruit_LEDBackpack.h" | |
#include "OneButton.h" | |
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4(); | |
#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 | |
// 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}; | |
int result; //ASCII Character code in decimal | |
void setup() { | |
Serial.begin(115200); | |
// wait for serial port to open | |
while (!Serial) delay(10); | |
alpha4.begin(0x70); // pass in the address | |
alpha4.writeDigitRaw(3, 0x0); | |
alpha4.writeDigitRaw(0, 0xFFFF); | |
alpha4.writeDisplay(); | |
delay(200); | |
alpha4.writeDigitRaw(0, 0x0); | |
alpha4.writeDigitRaw(1, 0xFFFF); | |
alpha4.writeDisplay(); | |
delay(200); | |
alpha4.writeDigitRaw(1, 0x0); | |
alpha4.writeDigitRaw(2, 0xFFFF); | |
alpha4.writeDisplay(); | |
delay(200); | |
alpha4.writeDigitRaw(2, 0x0); | |
alpha4.writeDigitRaw(3, 0xFFFF); | |
alpha4.writeDisplay(); | |
delay(200); | |
alpha4.clear(); | |
alpha4.writeDisplay(); | |
/* | |
// display every character, | |
for (uint8_t i='!'; i<='z'; i++) { | |
alpha4.writeDigitAscii(0, i); | |
alpha4.writeDigitAscii(1, i+1); | |
alpha4.writeDigitAscii(2, i+2); | |
alpha4.writeDigitAscii(3, i+3); | |
alpha4.writeDisplay(); | |
delay(300); | |
} | |
Serial.println("Start typing to display!"); | |
*/ | |
char displaybuffer[4] = {' ', ' ', ' ', ' '}; | |
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() { | |
for (uint8_t enc=0; enc<sizeof(found_encoders); enc++) { | |
if (found_encoders[enc] == false) continue; | |
int32_t new_position = encoders[enc].getEncoderPosition(); | |
// did we move around? | |
if (encoder_positions[enc] != new_position) { | |
Serial.print("Encoder #"); | |
Serial.print(enc); | |
Serial.print(" -> "); | |
//Serial.println(new_position); // display new position | |
Serial.write(result); //ASCII Char code in decimal | |
encoder_positions[enc] = new_position; | |
// 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"); | |
} | |
} | |
rotSwitch(); | |
} | |
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); | |
} | |
void rotSwitch() //Rotary encoder conversion to ASCII | |
{ | |
switch(encoder_positions) | |
{ | |
case 1: | |
{ | |
if(encoder_positions == 1) | |
{ | |
result = 65; // A | |
} | |
break; | |
} | |
case 2: | |
{ | |
if(encoder_positions == 2) | |
{ | |
result = 66; // B | |
} | |
break; | |
} | |
case 3: | |
{ | |
if(encoder_positions == 3) | |
{ | |
result = 67; // C | |
} | |
break; | |
} | |
case 4: | |
{ | |
if(encoder_positions == 4) | |
{ | |
result = 68; // D | |
} | |
break; | |
} | |
case 5: | |
{ | |
if(encoder_positions == 5) | |
{ | |
result = 69; // E | |
} | |
break; | |
} | |
case 6: | |
{ | |
if(encoder_positions == 6) | |
{ | |
result = 70; // F | |
} | |
break; | |
} | |
case 7: | |
{ | |
if(encoder_positions == 7) | |
{ | |
result = 71; // G | |
} | |
break; | |
} | |
case 8: | |
{ | |
if(encoder_positions == 8) | |
{ | |
result = 72; // H | |
} | |
break; | |
} | |
case 9: | |
{ | |
if(encoder_positions == 9) | |
{ | |
result = 73; // I | |
} | |
break; | |
} | |
case 10: | |
{ | |
if(encoder_positions == 10) | |
{ | |
result = 74; // J | |
} | |
break; | |
} | |
case 11: | |
{ | |
if(encoder_positions == 11) | |
{ | |
result = 75; // K | |
} | |
break; | |
} | |
case 12: | |
{ | |
if(encoder_positions == 12) | |
{ | |
result = 76; // L | |
} | |
break; | |
} | |
case 13: | |
{ | |
if(encoder_positions == 13) | |
{ | |
result = 77; // M | |
} | |
break; | |
} | |
case 14: | |
{ | |
if(encoder_positions == 14) | |
{ | |
result = 78; // N | |
} | |
break; | |
} | |
case 15: | |
{ | |
if(encoder_positions == 15) | |
{ | |
result = 79; //O | |
} | |
break; | |
} | |
case 16: | |
{ | |
if(encoder_positions == 16) | |
{ | |
result = 80; // P | |
} | |
break; | |
} | |
case 17: | |
{ | |
if(encoder_positions == 17) | |
{ | |
result = 81; // Q | |
} | |
break; | |
} | |
case 18: | |
{ | |
if(encoder_positions == 18) | |
{ | |
result = 82; // R | |
} | |
break; | |
} | |
case 19: | |
{ | |
if(encoder_positions == 19) | |
{ | |
result = 83; // S | |
} | |
break; | |
} | |
case 20: | |
{ | |
if(encoder_positions == 20) | |
{ | |
result = 84; // T | |
} | |
break; | |
} | |
case 21: | |
{ | |
if(encoder_positions == 21) | |
{ | |
result = 85; // U | |
} | |
break; | |
} | |
case 22: | |
{ | |
if(encoder_positions == 22) | |
{ | |
result = 86; // V | |
} | |
break; | |
} | |
case 23: | |
{ | |
if(encoder_positions == 23) | |
{ | |
result = 87; // W | |
} | |
break; | |
} | |
case 24: | |
{ | |
if(encoder_positions == 24) | |
{ | |
result = 88; // X | |
} | |
break; | |
} | |
case 25: | |
{ | |
if(encoder_positions == 25) | |
{ | |
result = 89; // Y | |
} | |
break; | |
} | |
case 26: | |
{ | |
if(encoder_positions == 26) | |
{ | |
result = 90; // Z | |
} | |
break; | |
} | |
case 27: | |
{ | |
if(encoder_positions == 27) | |
{ | |
result = 48; // 0 | |
} | |
break; | |
} | |
case 28: | |
{ | |
if(encoder_positions == 28) | |
{ | |
result = 49; // 1 | |
} | |
break; | |
} | |
case 29: | |
{ | |
if(encoder_positions == 29) | |
{ | |
result = 50; // 2 | |
} | |
break; | |
} | |
case 30: | |
{ | |
if(encoder_positions == 30) | |
{ | |
result = 51; // 3 | |
} | |
break; | |
} | |
case 31: | |
{ | |
if(encoder_positions == 31) | |
{ | |
result = 52; // 4 | |
} | |
break; | |
} | |
case 32: | |
{ | |
if(encoder_positions == 32) | |
{ | |
result = 53; // 5 | |
} | |
break; | |
} | |
case 33: | |
{ | |
if(encoder_positions == 33) | |
{ | |
result = 54; // 6 | |
} | |
break; | |
} | |
case 34: | |
{ | |
if(encoder_positions == 34) | |
{ | |
result = 55; // 7 | |
} | |
break; | |
} | |
case 35: | |
{ | |
if(encoder_positions == 35) | |
{ | |
result = 56; // 8 | |
} | |
break; | |
} | |
case 36: | |
{ | |
if(encoder_positions == 36) | |
{ | |
result = 57; // 9 | |
} | |
break; | |
} | |
} | |
} | |
/* | |
switch(encoder_positions) | |
^ | |
C:\Users\1234\Documents\Code for Tom's Project\TP_Project\TP_Project.ino:192:31: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] | |
if(encoder_positions == 1) | |
^ | |
Compilation error: switch quantity not an integer | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment