Last active
March 9, 2021 14:49
-
-
Save ghtomcat/2310711e162a780d2ee28308cfe0fd33 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
/************************************************************************** | |
read bme280 data and display on ssd1306 | |
**************************************************************************/ | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <AS5600.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#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); | |
Adafruit_BME280 bme; // I2C | |
AMS_5600 ams5600; | |
volatile byte revolutions; | |
int ang; | |
// interrupt routine to count revolutions | |
void IRAM_ATTR isr() { | |
revolutions += 1; | |
} | |
void setup() { | |
Wire.begin(); | |
Serial.begin(115200); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64 | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
if(ams5600.detectMagnet() == 0 ){ | |
while(1){ | |
if(ams5600.detectMagnet() == 1 ){ | |
Serial.print("Current Magnitude: "); | |
Serial.println(ams5600.getMagnitude()); | |
break; | |
} | |
else{ | |
Serial.println("Can not detect magnet"); | |
} | |
delay(1000); | |
} | |
} | |
unsigned status; | |
// default settings | |
status = bme.begin(0x76); | |
if (!status) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); | |
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16); | |
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); | |
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); | |
Serial.print(" ID of 0x60 represents a BME 280.\n"); | |
Serial.print(" ID of 0x61 represents a BME 680.\n"); | |
while (1) delay(10); | |
} | |
// attach pin 12 to interrupt routine | |
pinMode(12, INPUT); | |
attachInterrupt(12, isr, FALLING); | |
} | |
/******************************************************* | |
/* Function: convertRawAngleToDegrees | |
/* In: angle data from AMS_5600::getRawAngle | |
/* Out: human readable degrees as float | |
/* Description: takes the raw angle and calculates | |
/* float value in degrees. | |
/*******************************************************/ | |
float convertRawAngleToDegrees(word newAngle) | |
{ | |
/* Raw data reports 0 - 4095 segments, which is 0.087 of a degree */ | |
float retVal = newAngle * 0.087; | |
ang = retVal; | |
return ang; | |
} | |
void loop() { | |
display.clearDisplay(); | |
display.setTextSize(1); // Normal 1:1 pixel scale | |
display.setTextColor(SSD1306_WHITE); // Draw white text | |
display.setCursor(0,0); | |
display.print("Temperatur: "); | |
display.setCursor(70,0); | |
display.print(bme.readTemperature()); | |
display.setCursor(100,0); | |
display.print(" *C"); | |
display.setCursor(0,10); | |
display.print("Luftdruck: "); | |
display.setCursor(65,10); | |
display.print(bme.readPressure() / 100.0F); | |
display.setCursor(100,10); | |
display.print(" hPa"); | |
display.setCursor(0,20); | |
display.print("Feuchtigkeit: "); | |
display.setCursor(80,20); | |
display.print(bme.readHumidity()); | |
display.setCursor(110,20); | |
display.print(" %"); | |
display.setCursor(0,30); | |
display.print("Wind: "); | |
display.setCursor(50,30); | |
display.print(revolutions*6*16*0.001885); // revolutions in 60sec to km/h at 16cm diameter | |
display.setCursor(80,30); | |
display.print(" km/h"); | |
display.setCursor(0,40); | |
display.print("Wind: "); | |
display.setCursor(50,40); | |
display.print(convertRawAngleToDegrees(ams5600.getRawAngle())); | |
display.setCursor(80,40); | |
display.print(" Grad"); | |
display.display(); | |
revolutions=0; | |
// wait 10s | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment