Hey guys, I'm now in the electronic word hehe and trying to do a proyect of "The great resistor", but I have a problem, the oled blinks when I put a resistor, and it taked too much time to reed it, also sometimes the lecture is wrong. When it is read, the oled is frozen, I take off the resistor and the numbers stay on the oled, and ideas of what can it be?
I used 0,25mm conductors for conections, is a very thiny conductor.
#include <Adafruit_ADS1X15.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
uint16_t adc0, adc1, adc2, adc3;
float SumAvg,ResAvg;
int AvgCounts = 23, Loopcount=0, res;
float Resistor, adc0inmV, adc1inmV;
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
#define NUM_LEDS 17
#define BRIGHTNESS 150
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_RGB + NEO_KHZ800);
int gamma[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads; /* Use thi for the 12-bit version */
void setup(void)
{
Serial.begin(500000);
Serial.println("Say Hello to the great resistor!");
setupModules();
// testdrawchar();
rainbowCycle(2);
// colorWipe(strip.Color(255, 0, 0), 40); // Red
// colorWipe(strip.Color(0, 255, 0), 40); // Green
// colorWipe(strip.Color(0, 0, 255), 40); // Blue
whiteOverRainbow(1,50,2); // void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength )
delay(1000);
whiteBands(); // Background color of the resistor + tolerance band
}
void loop(void)
{
adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1)-26; // + offset
adc0inmV = adc0 * 0.1886; // adjusted by a bit
adc1inmV = adc1 * 0.1886;
Resistor=((adc1inmV*33250)/(adc0inmV-adc1inmV)); // resistor calulation
if(Resistor<=0)Resistor=0; // deal with nagavite resistor values
Loopcount++;
SumAvg += Resistor; // sum up readings
if (Loopcount == AvgCounts)
{
ResAvg=SumAvg/Loopcount; // clac average
Serial.print("ResAvg: "); Serial.println(ResAvg);
Serial.println(" ");
SumAvg=0;
Loopcount=0;
Resistor=ResAvg; // overriding noisy value with avearaged value
showResistorValueNeoPixel(); // update NeoPixel
showResistorValueSSD1306(); // update oled display
}
// delay(10);
}
float Temp;
void showResistorValueSSD1306()
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
if(Resistor<14000000)
{
display.print("R:");
display.println(Resistor,0);
display.println();
display.println(" [Ohm]");
Temp=map(Resistor,112000,70000,21900,31600);
// display.print(" "); display.print(Temp/1000,3);
display.display(); // Show initial text
whiteBands();
}
else // action when no resistor is interted
{
display.println(" insert ");
display.println(" Resistor");
display.display(); // Show initial text
rainbow(2);
//colorWipe(strip.Color(255, 0, 0), 40); // Red
//colorWipe(strip.Color(0, 255, 0), 40); // Green
//colorWipe(strip.Color(0, 0, 255), 40); // Blue
}
}
void showResistorValueNeoPixel()
{
//int led01 = map (Resistor,0,16000000,0,200);
//strip.setPixelColor(0, led01,0,200-led01);
//strip.setPixelColor(1, led01,0,200-led01);
// Multiplier Format: NEO_GRB
if (Resistor<1000) strip.setPixelColor(12, 0,0,0); // < 10 = Black
else
{
if (Resistor<10000) strip.setPixelColor(12, 75,120,25); // < 100 = Brown
else
{
if (Resistor<100000) strip.setPixelColor(12, 0,200,0); // < 1k = Red
else
{
if (Resistor<1000000) strip.setPixelColor(12, 100,200,0); // < 10k = Orange
else
{
if (Resistor<10000000) strip.setPixelColor(12, 200,200,0); // < 100k = Yellow
else
{
if (Resistor<100000000) strip.setPixelColor(12, 200,0,0); // < 1M = Green
else
{
if (Resistor<1000000000) strip.setPixelColor(12, 0,0,200); // < 10M = Blue
else
{
if (Resistor<10000000000) strip.setPixelColor(12, 0,200,200); // < 100M = Violet
else
{
if (Resistor<100000000000)strip.setPixelColor(12, 50,50,50); // < 1G = Grey
}
}
}
}
}
}
}
}
char Resistorbuf[12] = "";
dtostrf(Resistor,1,0,&Resistorbuf[strlen(Resistorbuf)]);
if(Resistor>99) // Values above 99 Ohms
{
// 1. Band Format: NEO_GRB
if (Resistorbuf[0]=='1')strip.setPixelColor(3, 75,120,25); // 1 = Brown
if (Resistorbuf[0]=='2')strip.setPixelColor(3, 0,200,0); // 2 = Red
if (Resistorbuf[0]=='3')strip.setPixelColor(3, 100,200,0); // 3 = Orange
if (Resistorbuf[0]=='4')strip.setPixelColor(3, 200,200,0); // 4 = Yellow
if (Resistorbuf[0]=='5')strip.setPixelColor(3, 200,0,0); // 5 = Green
if (Resistorbuf[0]=='6')strip.setPixelColor(3, 0,0,200); // 6 = Blue
if (Resistorbuf[0]=='7')strip.setPixelColor(3, 0,200,200); // 7 = Violet
if (Resistorbuf[0]=='8')strip.setPixelColor(3, 50,50,50); // 8 = Grey
if (Resistorbuf[0]=='9')strip.setPixelColor(3, 255,255,255); // 9 = White
// 2. Band Format: NEO_GRB
if (Resistorbuf[1]=='1')strip.setPixelColor(6, 75,120,25); // 1 = Brown
if (Resistorbuf[1]=='2')strip.setPixelColor(6, 0,200,0); // 2 = Red
if (Resistorbuf[1]=='3')strip.setPixelColor(6, 100,200,0); // 3 = Orange
if (Resistorbuf[1]=='4')strip.setPixelColor(6, 200,200,0); // 4 = Yellow
if (Resistorbuf[1]=='5')strip.setPixelColor(6, 200,0,0); // 5 = Green
if (Resistorbuf[1]=='6')strip.setPixelColor(6, 0,0,200); // 6 = Blue
if (Resistorbuf[1]=='7')strip.setPixelColor(6, 0,200,200); // 7 = Violet
if (Resistorbuf[1]=='8')strip.setPixelColor(6, 50,50,50); // 8 = Grey
if (Resistorbuf[1]=='9')strip.setPixelColor(6, 255,255,255); // 9 = White
// 3. Band Format: NEO_GRB
if (Resistorbuf[2]=='1')strip.setPixelColor(9, 75,120,25); // 1 = Brown
if (Resistorbuf[2]=='2')strip.setPixelColor(9, 0,200,0); // 2 = Red
if (Resistorbuf[2]=='3')strip.setPixelColor(9, 100,200,0); // 3 = Orange
if (Resistorbuf[2]=='4')strip.setPixelColor(9, 200,200,0); // 4 = Yellow
if (Resistorbuf[2]=='5')strip.setPixelColor(9, 200,0,0); // 5 = Green
if (Resistorbuf[2]=='6')strip.setPixelColor(9, 0,0,200); // 6 = Blue
if (Resistorbuf[2]=='7')strip.setPixelColor(9, 0,200,200); // 7 = Violet
if (Resistorbuf[2]=='8')strip.setPixelColor(9, 50,50,50); // 8 = Grey
if (Resistorbuf[2]=='9')strip.setPixelColor(9, 255,255,255); // 9 = White
if (Resistorbuf[0]=='0'||Resistor<=0)strip.setPixelColor(3, 0,0,0); // 0 = Black
if (Resistorbuf[1]=='0'||Resistor<10)strip.setPixelColor(6, 0,0,0); // 0 = Black
if (Resistorbuf[2]=='0'||Resistor<100)strip.setPixelColor(9, 0,0,0); // 0 = Black
}
else
{
if(Resistor>9) // move all shown values above 9 under 100 one to the right 79 -> 079
{
// 1. Band Format: NEO_GRB
strip.setPixelColor(3, 0,0,0); // 0 = Black
// 2. Band Format: NEO_GRB
if (Resistorbuf[0]=='1')strip.setPixelColor(6, 75,120,25); // 1 = Brown
if (Resistorbuf[0]=='2')strip.setPixelColor(6, 0,200,0); // 2 = Red
if (Resistorbuf[0]=='3')strip.setPixelColor(6, 100,200,0); // 3 = Orange
if (Resistorbuf[0]=='4')strip.setPixelColor(6, 200,200,0); // 4 = Yellow
if (Resistorbuf[0]=='5')strip.setPixelColor(6, 200,0,0); // 5 = Green
if (Resistorbuf[0]=='6')strip.setPixelColor(6, 0,0,200); // 6 = Blue
if (Resistorbuf[0]=='7')strip.setPixelColor(6, 0,200,200); // 7 = Violet
if (Resistorbuf[0]=='8')strip.setPixelColor(6, 50,50,50); // 8 = Grey
if (Resistorbuf[0]=='9')strip.setPixelColor(6, 255,255,255); // 9 = White
// 3. Band Format: NEO_GRB
if (Resistorbuf[1]=='1')strip.setPixelColor(9, 75,120,25); // 1 = Brown
if (Resistorbuf[1]=='2')strip.setPixelColor(9, 0,200,0); // 2 = Red
if (Resistorbuf[1]=='3')strip.setPixelColor(9, 100,200,0); // 3 = Orange
if (Resistorbuf[1]=='4')strip.setPixelColor(9, 200,200,0); // 4 = Yellow
if (Resistorbuf[1]=='5')strip.setPixelColor(9, 200,0,0); // 5 = Green
if (Resistorbuf[1]=='6')strip.setPixelColor(9, 0,0,200); // 6 = Blue
if (Resistorbuf[1]=='7')strip.setPixelColor(9, 0,200,200); // 7 = Violet
if (Resistorbuf[1]=='8')strip.setPixelColor(9, 50,50,50); // 8 = Grey
if (Resistorbuf[1]=='9')strip.setPixelColor(9, 255,255,255); // 9 = White
// if (Resistorbuf[0]=='0'||Resistor<10)strip.setPixelColor(6, 0,0,0); // 0 = Black
// if (Resistorbuf[1]=='0'||Resistor<100)strip.setPixelColor(9, 0,0,0); // 0 = Black
}
else
{
if (Resistor<10) // move all shown values above 0 under 10 one to the right 9 -> 009
{
strip.setPixelColor(3, 0,0,0); // 0 = Black
strip.setPixelColor(6, 0,0,0); // 0 = Black
if (Resistorbuf[0]=='1')strip.setPixelColor(9, 75,120,25); // 1 = Brown
if (Resistorbuf[0]=='2')strip.setPixelColor(9, 0,200,0); // 2 = Red
if (Resistorbuf[0]=='3')strip.setPixelColor(9, 100,200,0); // 3 = Orange
if (Resistorbuf[0]=='4')strip.setPixelColor(9, 200,200,0); // 4 = Yellow
if (Resistorbuf[0]=='5')strip.setPixelColor(9, 200,0,0); // 5 = Green
if (Resistorbuf[0]=='6')strip.setPixelColor(9, 0,0,200); // 6 = Blue
if (Resistorbuf[0]=='7')strip.setPixelColor(9, 0,200,200); // 7 = Violet
if (Resistorbuf[0]=='8')strip.setPixelColor(9, 50,50,50); // 8 = Grey
if (Resistorbuf[0]=='9')strip.setPixelColor(9, 255,255,255); // 9 = White
if (Resistorbuf[0]=='0')strip.setPixelColor(9, 0,0,0); // 0 = Black
}
}
}
strip.show();
}
void whiteBands()
{
strip.setPixelColor(0, 200,0,0);
strip.setPixelColor(1, 200,0,0);
strip.setPixelColor(2, 150,150,150);
strip.setPixelColor(4, 100,100,100);
strip.setPixelColor(5, 100,100,100);
strip.setPixelColor(7, 100,100,100);
strip.setPixelColor(8, 100,100,100);
strip.setPixelColor(10, 100,100,100);
strip.setPixelColor(11, 100,100,100);
strip.setPixelColor(13, 100,100,100);
strip.setPixelColor(14, 100,100,100);
strip.setPixelColor(15, 0,200,0); // Tolerance = 2% = Red
strip.setPixelColor(16, 150,150,150);
strip.show();
}
void setupModules()
{
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setRotation(2);
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(100); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
// display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
// delay(2000);
// display.display() is NOT necessary after every single drawing command,
// unless that's what you want...rather, you can batch up a bunch of
// drawing operations and then update the screen all at once by calling
// display.display(). These examples demonstrate both approaches...
// testdrawline(); // Draw many lines
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015 ADS1115
// ------- -------
ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
ads.begin();
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Some example procedures showing how to display to the pixels:
// colorWipe(strip.Color(255, 0, 0), 50); // Red
// colorWipe(strip.Color(0, 255, 0), 50); // Green
// colorWipe(strip.Color(0, 0, 255), 50); // Blue
// whiteOverRainbow(20,75,5);
}
////////////////////////////////////////////////// NeoPixel //////////////////////////////////////////////////
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void pulseWhite(uint8_t wait) {
for(int j = 0; j < 256 ; j++){
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0,0,0, gamma[j] ) );
}
delay(wait);
strip.show();
}
for(int j = 255; j >= 0 ; j--){
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0,0,0, gamma[j] ) );
}
delay(wait);
strip.show();
}
}
void rainbowFade2White(uint8_t wait, int rainbowLoops, int whiteLoops) {
float fadeMax = 100.0;
int fadeVal = 0;
uint32_t wheelVal;
int redVal, greenVal, blueVal;
for(int k = 0 ; k < rainbowLoops ; k ++){
for(int j=0; j<256; j++) { // 5 cycles of all colors on wheel
for(int i=0; i< strip.numPixels(); i++) {
wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255);
redVal = red(wheelVal) * float(fadeVal/fadeMax);
greenVal = green(wheelVal) * float(fadeVal/fadeMax);
blueVal = blue(wheelVal) * float(fadeVal/fadeMax);
strip.setPixelColor( i, strip.Color( redVal, greenVal, blueVal ) );
}
//First loop, fade in!
if(k == 0 && fadeVal < fadeMax-1) {
fadeVal++;
}
//Last loop, fade out!
else if(k == rainbowLoops - 1 && j > 255 - fadeMax ){
fadeVal--;
}
strip.show();
delay(wait);
}
}
delay(500);
for(int k = 0 ; k < whiteLoops ; k ++){
for(int j = 0; j < 256 ; j++){
for(uint16_t i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0,0,0, gamma[j] ) );
}
strip.show();
}
delay(2000);
for(int j = 255; j >= 0 ; j--){
for(uint16_t i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0,0,0, gamma[j] ) );
}
strip.show();
}
}
delay(500);
}
void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
int head = whiteLength - 1;
int tail = 0;
int loops = 3;
int loopNum = 0;
static unsigned long lastTime = 0;
while(true){
for(int j=0; j<256; j++) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
}
else{
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
}
if(millis() - lastTime > whiteSpeed) {
head++;
tail++;
if(head == strip.numPixels()){
loopNum++;
}
lastTime = millis();
}
if(loopNum == loops) return;
head%=strip.numPixels();
tail%=strip.numPixels();
strip.show();
delay(wait);
}
}
}
void fullWhite() {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
}
strip.show();
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256 * 5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
}
uint8_t red(uint32_t c) {
return (c >> 8);
}
uint8_t green(uint32_t c) {
return (c >> 16);
}
uint8_t blue(uint32_t c) {
return (c);
}
////////////////////////////////////////////////// OLED //////////////////////////////////////////////////
void testdrawline() {
int16_t i;
display.clearDisplay(); // Clear display buffer
for(i=0; i<display.width(); i+=4) {
display.drawLine(0, 0, i, display.height()-1, SSD1306_WHITE);
display.display(); // Update screen with each newly-drawn line
delay(1);
}
for(i=0; i<display.height(); i+=4) {
display.drawLine(0, 0, display.width()-1, i, SSD1306_WHITE);
display.display();
delay(1);
}
delay(250);
display.clearDisplay();
for(i=0; i<display.width(); i+=4) {
display.drawLine(0, display.height()-1, i, 0, SSD1306_WHITE);
display.display();
delay(1);
}
for(i=display.height()-1; i>=0; i-=4) {
display.drawLine(0, display.height()-1, display.width()-1, i, SSD1306_WHITE);
display.display();
delay(1);
}
delay(250);
display.clearDisplay();
for(i=display.width()-1; i>=0; i-=4) {
display.drawLine(display.width()-1, display.height()-1, i, 0, SSD1306_WHITE);
display.display();
delay(1);
}
for(i=display.height()-1; i>=0; i-=4) {
display.drawLine(display.width()-1, display.height()-1, 0, i, SSD1306_WHITE);
display.display();
delay(1);
}
delay(250);
display.clearDisplay();
for(i=0; i<display.height(); i+=4) {
display.drawLine(display.width()-1, 0, 0, i, SSD1306_WHITE);
display.display();
delay(1);
}
for(i=0; i<display.width(); i+=4) {
display.drawLine(display.width()-1, 0, i, display.height()-1, SSD1306_WHITE);
display.display();
delay(1);
}
delay(2000); // Pause for 2 seconds
}
void testdrawrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2; i+=2) {
display.drawRect(i, i, display.width()-2*i, display.height()-2*i, SSD1306_WHITE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(2000);
}
void testfillrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2; i+=3) {
// The INVERSE color is used so rectangles alternate white/black
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(2000);
}
void testdrawcircle(void) {
display.clearDisplay();
for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) {
display.drawCircle(display.width()/2, display.height()/2, i, SSD1306_WHITE);
display.display();
delay(1);
}
delay(2000);
}
void testfillcircle(void) {
display.clearDisplay();
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
// The INVERSE color is used so circles alternate white/black
display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE);
display.display(); // Update screen with each newly-drawn circle
delay(1);
}
delay(2000);
}
void testdrawroundrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2-2; i+=2) {
display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i,
display.height()/4, SSD1306_WHITE);
display.display();
delay(1);
}
delay(2000);
}
void testfillroundrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2-2; i+=2) {
// The INVERSE color is used so round-rects alternate white/black
display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i,
display.height()/4, SSD1306_INVERSE);
display.display();
delay(1);
}
delay(2000);
}
void testdrawtriangle(void) {
display.clearDisplay();
for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
display.drawTriangle(
display.width()/2 , display.height()/2-i,
display.width()/2-i, display.height()/2+i,
display.width()/2+i, display.height()/2+i, SSD1306_WHITE);
display.display();
delay(1);
}
delay(2000);
}
void testfilltriangle(void) {
display.clearDisplay();
for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) {
// The INVERSE color is used so triangles alternate white/black
display.fillTriangle(
display.width()/2 , display.height()/2-i,
display.width()/2-i, display.height()/2+i,
display.width()/2+i, display.height()/2+i, SSD1306_INVERSE);
display.display();
delay(1);
}
delay(2000);
}
void testdrawchar(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
// display.cp437(true); // Use full 256 char 'Code Page 437' font
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
for(int16_t i=0; i<256; i++) {
if(i == '\n') display.write(' ');
else display.write(i);
}
display.display();
delay(2000);
}
void testdrawstyles(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println(F("Hello, world!"));
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.println(3.141592);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.print(F("0x")); display.println(0xDEADBEEF, HEX);
display.display();
delay(2000);
}
void testscrolltext(void) {
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("scroll"));
display.display(); // Show initial text
delay(100);
// Scroll in various directions, pausing in-between:
display.startscrollright(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(1000);
display.startscrolldiagright(0x00, 0x07);
delay(2000);
display.startscrolldiagleft(0x00, 0x07);
delay(2000);
display.stopscroll();
delay(1000);
}
void testdrawbitmap(void) {
display.clearDisplay();
display.drawBitmap(
(display.width() - LOGO_WIDTH ) / 2,
(display.height() - LOGO_HEIGHT) / 2,
logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
delay(1000);
}
#define XPOS 0 // Indexes into the 'icons' array in function below
#define YPOS 1
#define DELTAY 2
void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
int8_t f, icons[NUMFLAKES][3];
// Initialize 'snowflake' positions
for(f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
icons[f][YPOS] = -LOGO_HEIGHT;
icons[f][DELTAY] = random(1, 6);
Serial.print(F("x: "));
Serial.print(icons[f][XPOS], DEC);
Serial.print(F(" y: "));
Serial.print(icons[f][YPOS], DEC);
Serial.print(F(" dy: "));
Serial.println(icons[f][DELTAY], DEC);
}
for(;;) { // Loop forever...
display.clearDisplay(); // Clear the display buffer
// Draw each snowflake:
for(f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
}
display.display(); // Show the display buffer on the screen
delay(200); // Pause for 1/10 second
// Then update coordinates of each flake...
for(f=0; f< NUMFLAKES; f++) {
icons[f][YPOS] += icons[f][DELTAY];
// If snowflake is off the bottom of the screen...
if (icons[f][YPOS] >= display.height()) {
// Reinitialize to a random position, just off the top
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
icons[f][YPOS] = -LOGO_HEIGHT;
icons[f][DELTAY] = random(1, 6);
}
}
}
}
