Skip to content

Instantly share code, notes, and snippets.

@pdp7
Last active February 3, 2016 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdp7/2240a4c27c07c617f932 to your computer and use it in GitHub Desktop.
Save pdp7/2240a4c27c07c617f932 to your computer and use it in GitHub Desktop.
Arduino sketch: osh-park-teensy-knob-oled
/*
AnalogReadSerial modified to write to Adafruit i2c OLED display
Reads an analog input on pin 0, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// If using software SPI (the default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
}
// the loop routine runs over and over again forever:
void loop() {
char buf[5];
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
sensorValue = sensorValue & 0xFFF0;
int mapValue = map(sensorValue, 0, 1008, 0, 128);
int printValue = map(sensorValue, 0, 1008, 0, 1024);
// print out the value you read:
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(4);
sprintf(buf, "%04d", printValue);
display.println(buf);
display.fillRect(0, 32, mapValue, 32, 1);
display.display();
delay(10); // delay in between reads for stability
}
@pdp7
Copy link
Author

pdp7 commented Feb 3, 2016

Notes from Teensy 3.2 setup on Debianb 9 (Stretch) x86_64:
https://gist.github.com/pdp7/ba2622da1f9223c6f8cd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment