code: /* #### Connection Info: #### ## Nucleo -> ALS 1 (physical closest to nucleo): ##…
- User
code: /* | #### Connection Info: #### | | ## Nucleo -> ALS 1 (physical closest to nucleo): ## | D9 -> 1(CS) | D12 -> 3(SDO) | D13 -> 4(SCK) | ( VCC 3.3V Rail and GND Rail -> VCC and GND) | | ## Nucleo -> ALS 2 (physical furthest from nucleo): ## | D8 -> 1(CS) | D7 -> 3(SDO) | D13 -> 4(SCK) (Shared with ALS1 and Pmod OLEDrgb) | ( VCC 3.3V Rail and GND Rail -> VCC and GND) | | ## Nucleo -> Pmod OLEDrgb: ## | A7 -> 1(CS) | D11 -> 2(MOSI) | D13 -> 4(SCK) | D10 -> 7(DC) | A4 -> 8(RES) | A6 -> 9(VCCEN) | A5 -> 10(PMODEN) | ( VCC 5V Rail and GND Rail -> VCC and GND) | | ## Nucleo -> MCP9700A: ## | A2 -> 2(V_OUT) | GND Rail -> 3(GND) | 3.3V VCC Rail -> 1(V_DD) | | ## Nucleo -> MOD-WIFI-ESP8266 ## | D4 -> 4(ESP tx) | D3 -> 3(ESP rx) | 3.3V VCC Rail -> 1 (VCC) | GND Rail -> 2 (GND) | | Nucleo -> Green LED | D5 | | #### Version Info #### | Version Number: 1.0 | Author: Andreas Kjersheim | Date: 20.12.2023 | | Source: McLab10_OLEDrgb_L432KC_OS60_tk2, Timo Karppinen 3.10.2022 SPDX-License-Identifier: Apache-2.0 | */
#include "mbed.h" #include "Adafruit_SSD1331.h"
#include "Adafruit_GFX.h"/* ----------- Pmod OLEDrgb declarations and definitions ------------------ */
Adafruit_SSD1331 OLED(A7, A4, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck DigitalOut VCCEN(A6); DigitalOut PMODEN(A5); int first = 0;
// Definition of colours on the OLED display #define Black 0x0000 #define Blue 0x001F #define Red 0xF800 #define Green 0x07E0 #define Cyan 0x07FF #define Magenta 0xF81F #define Yellow 0xFFE0 #define White 0xFFFF
/* ----------- Time declarations and definitions ------------------ */
char Time[32]; void getTime();
void getTime() { time_t seconds = time(NULL); // https://os.mbed.com/docs/mbed-os/v6.7/apis/time.html strftime(Time,32,"%I:%M:%p\n", localtime(&seconds)); }
/* ----------- Temperature Sensor MCP9700E and Sub Routine readTemperature ------------------ */
AnalogIn tempSensor(A2); // Connected to pin A2
int readTemperature() { float voltage = tempSensor.read() * 3.3f; // Convert analog reading to voltage int temperature = (int)((voltage - 0.5f) * 100.0f); // Convert voltage to int temperature return temperature; }
float temperature = readTemperature(); // Get temperature reading char buffer[32]; // Buffer to hold the formatted string
DigitalOut greenLED(D5); // Green LED connected to pin D5
/* ----------- Pmod ALS Sensor declarations and sub routine ------------------ */ // ALS 1 SPI spi(D11, D12, D13); // SPI pins for ALS sensor DigitalOut alsCS(D9); // Chip select for sensor SPI communication
// ALS 2 //SPI spi2(D11, D12, D13); // SPI pins for ALS sensor 2 //DigitalOut alsCS2(D8); // Chip select for sensor 2 SPI communication
const int numReadings = 10; // Number of readings for moving average int readings[numReadings]; // Array to store individual sensor readings int readIndex = 0; // Index of the current reading float total = 0; // Running total float average = 0; // Average
float exponentialMovingAverage(float newSample, float existingAverage, float alpha) { return alpha * newSample + (1 - alpha) * existingAverage; }
int getALS() { char alsByte0 = 0; char alsByte1 = 0; int als8bit = 0; float alsScaled = 0.0;
alsCS.write(0); // Begin the conversion process alsByte0 = spi.write(0x00); alsByte1 = spi.write(0x00); alsCS.write(1); // End of conversion als8bit = (alsByte0 << 4) | (alsByte1 >> 4);static float average = 0; float alpha = 0.1; // Smoothing factor, adjust as needed
average = exponentialMovingAverage(als8bit, average, alpha); // Scale the average value to Lux float alsScaled = average * 0.23; // Adjust scaling factor based on calibration return (int)alsScaled;}
int main(){ VCCEN = 1; // not connected VCCEN permanently to Vcc PMODEN = 1; // not connected VCCEN permanently to Vcc
set_time(1703078697); // Setting RTC time from https://www.epochconverter.com/ // Giving the program some time before starting the display ThisThread::sleep_for(500ms); OLED.begin(); // initialization of display object OLED.clearScreen(); // SPI setup for the ALS sensor 1 spi.format(8, 0); spi.frequency(12000000); // SPI setup for ALS sensor 2 //spi2.format(8, 0); //spi2.frequency(12000000); while(true){ while(first < 1){ // Testing/Display of a sequence on the OLED screen 3 times first += 1; OLED.fillScreen(Black); // background screen in black OLED.setTextColor(Cyan); // colour of text in cyan OLED.setCursor(0,0); // cursor is in x=0 and y=15 OLED.printf("Test module Pmod"); // display text ThisThread::sleep_for(500ms); // wait 500 ms OLED.setCursor(0,15); // cursor is in x=0 and y=15 OLED.setTextSize(2); // size of text OLED.setTextColor(Red); // text in red colour OLED.printf("DIGILENT"); // display text OLED.setCursor(20,40); // cursor is in x=20 and y=40 OLED.setTextSize(1); // size of text OLED.setTextColor(Green); // text in green colour OLED.printf("LEXTRONIC"); // display text OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); //blue line x=1, width-1 and y=60 ThisThread::sleep_for(2000ms); // wait 2 s OLED.fillScreen(Black); // background display in black (erase display) OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge OLED.fillRoundRect(35, 5, 30, 40, 1, White); OLED.fillRoundRect(65, 5, 30, 40, 1, Red); OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55 ThisThread::sleep_for(1000ms); // wait 1 s } int temperature = readTemperature(); // Get updated temperature reading getTime(); // Get the current time int lux = getALS(); //int lux2 = getALS(alsCS2, spi2); // Check if temperature is between 18 and 25 degrees Celsius if (temperature >= 18 && temperature <= 25) { greenLED = 1; // Turn on the LED } else { greenLED = 0; // Turn off the LED } OLED.clearScreen(); OLED.fillScreen(Black); OLED.setTextColor(White); OLED.setCursor(0, 0); OLED.printf("Time: %s", Time); OLED.setCursor(0, 15); OLED.printf("Temp: %d C", temperature); OLED.setCursor(0, 30); OLED.printf("Lux 1: %d\r\n", lux); //OLED.printf("Lux Sensor 2: %d\r\n", lux2); ThisThread::sleep_for(1500ms); // Update every second }}
the variable alsScaled is not properly set it seems due to an error. can you fix the issue
Assistant
It looks like there is an issue with declaring and initializing the average variable in the getALS() function. Here is a fix:
float getALS() { char alsByte0 = 0; char alsByte1 = 0; int als8bit = 0; float alsScaled = 0.0; alsCS.write(0); // Begin the conversion process alsByte0 = spi.write(0x00); alsByte1 = spi.write(0x00); alsCS.write(1); // End of conversion als8bit = (alsByte0 << 4) | (alsByte1 >> 4); float average; // Declare average variable float alpha = 0.1; // Smoothing factor, adjust as needed average = exponentialMovingAverage(als8bit, average, alpha); // Initialize average on first call // Rest of function }By declaring average inside the function, it will be initialized properly on the first call to getALS(). Before, it was using an uninitialized global variable which caused incorrect results.
Let me know if this fixes the issue with alsScaled!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.