Hi all, I'm still learning my way around Arduinos and basic coding. I have stitch together the code below that runs on an Arduino Nano. The problem I have is the SH1106 OLED only gets updated with the temperature only once when I power on the board. The fading RGB strip portion of the code works fine though. Seems like the code is not looping to continue to update temperature. Any help will greatly appreciated.
#include <math.h>
#include "Arduino.h"
#include "Thermistor.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_NeoPixel.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
#define PIN 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
unsigned int Rs = 10000;
double Vcc = 3.3;
void setup() {
strip.begin();
strip.setBrightness(255);
strip.show(); // Initialize all pixels to 'off'
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
}
void loop() {
LCD();
LED();
}
void LCD() {
display.setTextSize(1);
display.setCursor(2, 4);
display.print(" System Temperature ");
display.setTextSize(2);
display.setCursor(30, 35);
display.print(Thermister(AnalogRead()), 1); display.println(" F");
display.drawLine(0, 16, 128, 16, WHITE);
display.drawCircle(86, 34, 2, WHITE);
display.display();
delay(1000);
display.clearDisplay();
}
int AnalogRead() {
int val = 0;
for(int i = 0; i < 20; i++) {
val += analogRead(A0);
delay(1);
}
val = val / 20;
return val;
}
double Thermister(int val) {
double V_NTC = (double)val / 1024;
double R_NTC = (Rs * V_NTC) / (Vcc - V_NTC);
R_NTC = log(R_NTC);
double Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * R_NTC * R_NTC ))* R_NTC );
Temp = Temp - 273.15;
return Temp;
}
void LED() {
int TOTAL_LEDS = 8;
float MaximumBrightness = 255;
float SpeedFactor = 0.008;
float StepDelay = 3;
// Make the lights breathe
for (int i = 0; i < 65535; i++) {
float intensity = MaximumBrightness /2.0 * (1.0 + sin(SpeedFactor * i));
strip.setBrightness(intensity);
for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {
strip.setPixelColor(ledNumber, 0, 255, 0);
}
strip.show();
delay(StepDelay);
}
}