Hi folks, so learning C, using DeepSeek to help me learn, as I feel this is the best course of action for me.
DS has produced some reasonable attempts so far, although it does seem to make very basic, dare I say human errors at times!
So in this example snippets of code, why would one code never produce a display error (display works) whilst another piece of code always produces a display error and will not display anything...? Is this a compiling issue?
DS has produced some reasonable attempts so far, although it does seem to make very basic, dare I say human errors at times!
So in this example snippets of code, why would one code never produce a display error (display works) whilst another piece of code always produces a display error and will not display anything...? Is this a compiling issue?
None working example - always reports disconnected display :
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <EEPROM.h>
// =============================================
// Hardware Configuration
// =============================================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3D // I2C address for OLED
#define OLED_RESET 10 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BME280_ADDRESS 0x76 // I2C address for BME280
Adafruit_BME280 bme;
// Button pins (using internal pull-up)
#define BUTTON_PLUS 2
#define BUTTON_MINUS 3
#define BUTTON_ENTER 4
// Output pins
#define HUM_OUTPUT_PIN 9 // PWM output for humidity control
#define ALARM_OUTPUT_PIN 7 // Digital output for alarm
// =============================================
// Default Settings (adjustable)
// =============================================
#define DEFAULT_TARGET_HUM 55 // Default target hum
#define DEFAULT_HIGH_T_ALARM 35 // Default high talarm
#define DEFAULT_LOW_TEMP_ALARM 15 // Default low t alarm
#define DEFAULT_ALARM_ENABLED true // Default alarm state
#define PWM_FREQUENCY 108000 // PWM frequency in Hz (108kHz)
#define PWM_DUTY_CYCLE 10 // Default PWM duty cycle (10%)
// =============================================
// EEPROM Addresses
// =============================================
#define EEPROM_TARGET_HUMIDITY 0 // 1 byte
#define EEPROM_HIGH_TEMP_ALARM 1 // 1 byte
#define EEPROM_LOW_TEMP_ALARM 2 // 1 byte
#define EEPROM_ALARM_ENABLED 3 // 1 byte
// =============================================
// System Parameters (don't change)
// =============================================
#define HUMIDITY_MIN 1
#define HUMIDITY_MAX 65
#define TEMP_MIN 4
#define TEMP_MAX 65
#define ENTER_HOLD_TIME 1000 // 1 second hold time for enter button
#define BUTTON_DEBOUNCE 50 // Debounce time in ms
#define FAST_REPEAT_DELAY 3000 // Time before button repeat speeds up (ms)
// =============================================
// Global Variables
// =============================================
struct SystemSettings {
uint8_t targetHumidity;
uint8_t highTempAlarm;
uint8_t lowTempAlarm;
bool highAlarmEnabled;
bool lowAlarmEnabled;
};
SystemSettings settings;
// Button states
bool buttonPlusPressed = false;
bool buttonMinusPressed = false;
bool buttonEnterPressed = false;
unsigned long buttonPlusTime = 0;
unsigned long buttonMinusTime = 0;
unsigned long buttonEnterTime = 0;
// System state
enum MenuState { MAIN_SCREEN, SET_HUMIDITY, SET_HIGH_TEMP, SET_LOW_TEMP, SET_ALARM_ENABLE };
MenuState currentMenu = MAIN_SCREEN;
bool outputActive = false;
bool alarmActive = false;
bool tempAlarmFlash = false;
unsigned long lastFlashTime = 0;
const int flashInterval = 500; // Flash interval for alarm (ms)
// =============================================
// Setup Function
// =============================================
void setup() {
// Initialize serial communication
Serial.begin(9600);
Serial.println(F("Humidistat"));
// Initialize OLED
pinMode(OLED_RESET, OUTPUT);
digitalWrite(OLED_RESET, LOW);
delay(10);
digitalWrite(OLED_RESET, HIGH);
delay(10);
// Initialize display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Initialize BME280 sensor
if (!bme.begin(BME280_ADDRESS)) {
Serial.println(F("Could not find a valid BME280 sensor, check wiring!"));
while (1);
}
// Set up buttons with internal pull-up
pinMode(BUTTON_PLUS, INPUT_PULLUP);
pinMode(BUTTON_MINUS, INPUT_PULLUP);
pinMode(BUTTON_ENTER, INPUT_PULLUP);
// Set up outputs
pinMode(HUM_OUTPUT_PIN, OUTPUT);
pinMode(ALARM_OUTPUT_PIN, OUTPUT);
// Configure PWM frequency for D9
TCCR1A = _BV(COM1A1) | _BV(WGM10); // Fast PWM, 8-bit
TCCR1B = _BV(WGM12) | _BV(CS10); // No prescaler (16MHz/1 = 16MHz)
OCR1A = (PWM_DUTY_CYCLE * 255) / 100; // Set duty cycle
// Load settings from EEPROM or use defaults
loadSettings();
// Initial display
display.clearDisplay();
display.display();
}
Working example - ignores disconnected display:
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BME280.h>
#include <EEPROM.h>
// Pin Definitions
#define BUTTON_UP 2 // Button 1 - Count up
#define BUTTON_DOWN 3 // Button 2 - Count down
#define BUTTON_SELECT 4 // Button 3 - Select
#define OUTPUT_PIN 5 // Main control output pin
#define PULSE_OUTPUT_PIN 9 // 108kHz pulsed output (must be Timer1 compatible)
#define OLED_RESET 10 // Reset pin for OLED
// Display Settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// EEPROM Address Definitions
#define EEPROM_ADDR_TARGET 0
#define EEPROM_ADDR_PULSE 2
// Sensor
Adafruit_BME280 bme;
// Constants
#define HYSTERESIS 2.0 // ±2% hysteresis
#define MIN_PULSE_WIDTH 1 // 1% minimum duty cycle
#define MAX_PULSE_WIDTH 50 // 50% maximum duty cycle
#define PULSE_FREQUENCY 108000 // 108kHz frequency
#define DEBOUNCE_DELAY 50 // Button debounce time (ms)
#define LONG_PRESS_TIME 1000 // Long press duration (ms)
#define STARTUP_PRESS_TIME 2000 // Press time during startup to enter pulse settings
#define INITIAL_SCROLL_DELAY 300 // Delay before fast scrolling starts (ms)
#define FAST_SCROLL_INTERVAL 100 // Interval during fast scrolling (ms)
// Global Variables
float currentTemp = 0;
float currentHumidity = 0;
int targetHumidity = 60; // Default target humidity
int pulseWidthPercent = 10; // Default pulse width (10%)
bool settingMode = false;
bool pulseSettingMode = false; // Startup settings mode
bool outputState = false;
unsigned long lastDisplayUpdate = 0;
unsigned long lastBlink = 0;
unsigned long lastButtonCheck = 0;
const unsigned long buttonCheckInterval = 20; // Check buttons every 20ms
bool blinkState = false;
// Function declarations
void loadSettings();
void saveSettings();
void readSensorData();
void checkHumidity();
void updatePulseOutput();
void handleButtons();
void updateDisplay();
void adjustValue(int direction);
void setup() {
Wire.setClock(100000L);
Serial.begin(9600);
Serial.println(F("Humidistat"));
// Initialize OLED
pinMode(OLED_RESET, OUTPUT);
digitalWrite(OLED_RESET, LOW);
delay(10);
digitalWrite(OLED_RESET, HIGH);
delay(10);
// Initialize pins
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
pinMode(OUTPUT_PIN, OUTPUT);
digitalWrite(OUTPUT_PIN, LOW);
// Improved startup button check
bool enteredPulseMode = false;
unsigned long buttonPressStart = millis();
// Check if button is pressed at startup (with debounce)
if (digitalRead(BUTTON_SELECT) == LOW) {
delay(50); // Initial debounce delay
// If still pressed after debounce, wait for hold time
while (digitalRead(BUTTON_SELECT) == LOW) {
if (millis() - buttonPressStart > STARTUP_PRESS_TIME) {
enteredPulseMode = true;
pulseSettingMode = true;
break;
}
}
// Wait for button release with debounce
while (digitalRead(BUTTON_SELECT) == LOW); // Wait for physical release
delay(50); // Debounce release
}
// Configure Timer1 for 108kHz PWM on pin 9
pinMode(PULSE_OUTPUT_PIN, OUTPUT);
TCCR1A = _BV(COM1A1) | _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
ICR1 = F_CPU / PULSE_FREQUENCY;
OCR1A = 0;
// Initialize BME280 sensor
if (!bme.begin(0x76)) {
Serial.println(F("Could not find BME280 sensor!"));
while (1);
}
