Created
November 5, 2023 12:14
-
-
Save mariopesch/4261e0f24d07a7d10ad518842cbb86ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#include <Wire.h> | |
#include "SSD1306Ascii.h" | |
#include "SSD1306AsciiWire.h" | |
int SwitchState = 0; | |
byte error, address; | |
int nDevices = 0; | |
Adafruit_NeoPixel rgb_led(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); | |
SSD1306AsciiWire oled; | |
#define I2C_ADDRESS 0x3D | |
bool displayConnected = 0; | |
void i2c_scan() { | |
Serial.println("Scanning for I2C devices on QWIIC Ports ..."); | |
for (address = 0x01; address < 0x7f; address++) { | |
Wire.beginTransmission(address); | |
error = Wire.endTransmission(); | |
if (error == 0) { | |
Serial.printf("I2C device found at address 0x%02X\n", address); | |
nDevices++; | |
if (address == 0x3D) | |
{ | |
displayConnected = 1; | |
} | |
} else if (error != 2) { | |
Serial.printf("Error %d at address 0x%02X\n", error, address); | |
} | |
} | |
if (nDevices == 0) { | |
Serial.println("No I2C devices found"); | |
} | |
if (displayConnected == 1){ | |
oled.begin(&Adafruit128x64, I2C_ADDRESS); | |
oled.setFont(Adafruit5x7); | |
oled.println("Hello senseBox"); | |
delay(2000); | |
oled.clear(); | |
oled.println("Found"); | |
oled.print(nDevices); | |
oled.println("connected"); | |
} | |
Serial.println("Scanning for I2C on Wire1"); | |
delay(1000); | |
for (address = 0x01; address < 0x7f; address++) { | |
Wire1.beginTransmission(address); | |
error = Wire1.endTransmission(); | |
if (error == 0) { | |
Serial.printf("I2C device found at address 0x%02X\n", address); | |
nDevices++; | |
} else if (error != 2) { | |
Serial.printf("Error %d at address 0x%02X\n", error, address); | |
} | |
} | |
if (nDevices == 0) { | |
Serial.println("No I2C devices found"); | |
} | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
Wire.begin(); | |
Wire1.begin(); | |
rgb_led.begin(); | |
rgb_led.setBrightness(30); | |
i2c_scan(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
switch (SwitchState % 5) { | |
case 0: | |
rgb_led.setPixelColor(0, rgb_led.Color(255, 0, 0)); | |
rgb_led.show(); | |
SwitchState++; | |
break; | |
case 1: | |
rgb_led.setPixelColor(0, rgb_led.Color(0, 255, 0)); | |
rgb_led.show(); | |
SwitchState++; | |
break; | |
case 2: | |
rgb_led.setPixelColor(0, rgb_led.Color(0, 0, 255)); | |
rgb_led.show(); | |
SwitchState++; | |
break; | |
case 3: | |
Serial.println("Testing Lightsensor..."); | |
int pd_value; | |
Serial.println("Taking 20 samples..."); | |
for (int count = 0; count <20; count++) | |
{ | |
pd_value += analogRead(PD_SENSE); | |
delay(100); | |
} | |
delay(100); | |
Serial.println((pd_value/20)); | |
SwitchState++; | |
break; | |
default: | |
rgb_led.setPixelColor(0, rgb_led.Color(255, 255, 0)); | |
rgb_led.show(); | |
SwitchState = 0; | |
break; | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment