Skip to content

Instantly share code, notes, and snippets.

@nickcjohnston
Last active November 27, 2018 14:29
Show Gist options
  • Save nickcjohnston/2b69b8762f146d6f0c3a73b1f52b5578 to your computer and use it in GitHub Desktop.
Save nickcjohnston/2b69b8762f146d6f0c3a73b1f52b5578 to your computer and use it in GitHub Desktop.
ATTiny85, OLED, NeoPixel
#include <Tiny4kOLED.h> //needs TinyWireM
#include <Adafruit_NeoPixel.h>
// NeoPixel variables
#define N_LEDS 8
#define LED_PIN 1
#define LED_BRIGHTNESS 127
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
// Pins
#define RED A2 //Pin 3
#define GREEN A3 //Pin 2
#define BLUE A0 //Pin 1 (note, must disable reset pin w/avrdude -U hfuse:w:0x5F:m)
int red, green, blue = 0;
void setup() {
// Send the initialization sequence to the oled. This leaves the display turned off
oled.begin();
oled.clear();
oled.on();
// Switch the half of RAM that we are writing to, to be the half that is non currently displayed
oled.switchRenderFrame();
strip.begin();
strip.setBrightness(LED_BRIGHTNESS);
}
// Print leading zeros to keep display nice and aligned
void padInt(int val){
if (val < 10) {
oled.print("0");
}
if (val < 100) {
oled.print("0");
}
}
// Print leading zeros to keep display nice and aligned
void padHex(int val){
oled.print("0x");
if (val < 16) {
oled.print("0");
}
}
void loop() {
// Clear the non-displayed half of the memory to all black
// (The previous clear only cleared the other half of RAM)
oled.clear();
// Read potentiometer values
red = analogRead(RED);
green = analogRead(GREEN);
blue = analogRead(BLUE);
// Map the potentiometer value (0-1023) to an rgb value (0-255)
red = map(red, 0, 1023, 0, 255);
green = map(green, 0, 1023, 0, 255);
blue = map(blue, 0, 1023, 0, 255);
// The characters in the 8x16 font are 8 pixels wide and 16 pixels tall
// 2 lines of 16 characters exactly fills 128x32
oled.setFont(FONT8X16);
oled.setCursor(0, 0);
// Print RGB values
oled.print("R");
padInt(red);
oled.print(red);
oled.print(" G");
padInt(green);
oled.print(green);
oled.print(" B");
padInt(blue);
oled.print(blue);
// Print hex values
oled.setCursor(0, 2);
padHex(red);
oled.print(red, HEX);
oled.print(" ");
padHex(green);
oled.print(green, HEX);
oled.print(" ");
padHex(blue);
oled.print(blue, HEX);
// Swap which half of RAM is being written to, and which half is being displayed
oled.switchFrame();
// Set neopixel colour and display
strip.fill(strip.Color(red, green, blue), 0, N_LEDS);
strip.show();
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment