Good morning John.
The array...or list...does have decimals. I had realized this when I was trying to fall asleep last night (when the mind reminds you of all the items your forgot or never thought of). My bad. What I understand of this line of code is:
" Based on the code snippet int8_t KNOBDIR[] = { };, here's a breakdown of what it means in C/C++:
int8_t: This is a data type that represents an 8-bit signed integer. It can store values ranging from -128 to 127. This specific type, defined in the stdint.h header, ensures portability and consistent behavior across different platforms and compilers because it has a fixed size of 8 bits.
KNOBDIR: This is the name given to the array being declared.
[] = { };: This part declares an array and initializes it with an empty initializer list."
That is a direct copy/paste from the AI I used to understand this line better. How it ties in, I'm unsure. After running my code several times, I don't find it even matching any of the numbers in the array/list...even the integer numbers.
const int8_t KNOBDIR[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; // Example from a rotary encoder library
This is the initial line of code AI gave me. I made this line mine by inserting the 16 points of the compass values (360 / 16 = 22.5). That was my logic there.
Int8_t stores 8 bits of information and can hold from -128 to 127 range of values. I have 16 pieces of information...my assumption is that I am in the clear as I have never worked with this before.
I did open up the links you provided and outside of technical data, found nothing to further me along in my quest.
const int8_t KNOBDIR[] = {0, 23, 45, 68, 90, 113, 135, 158, 180, 203, 225, 248, 270, 293, 315, 338};
I've changed my line to reflect integers per your suggestion
#include <DS3231.h>
#include <ArduinoJson.h>
#include <DFRobot_VEML6075.h>
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Arduino.h>
#include <time.h>
#define DHTTYPE DHT22
Adafruit_BMP085 bmp;
#define VEML6075_ADDR 0x10
#define DEBOUNCE_TIME 80
#define CALC_INTERVAL 1000
#define ENCODER_A 2
DFRobot_VEML6075_IIC VEML6075(&Wire, VEML6075_ADDR);
DS3231 myRTC;
const int encoderPin_1 = 3;
const int encoderPin_2 = 4;
const int t_pin = 22;
const int h_pin = 24;
const int l_pin = A1;
const int rain_pin = 25;
OneWire oneWire(t_pin);
DallasTemperature sensors(&oneWire);
DHT dht(h_pin, DHTTYPE);
float p = 0;
float press = 0;
float temp_f = 0;
float rainfall = 0;
float trigger = 0;
float rpm = 0;
int dir = 0;
volatile long encoderTicks = 0;
volatile long encoderPosition = 0;
unsigned long startTime, elapsedTime;
unsigned long lastTime = 0;
long lastTicks = 0;
void setup() {
Serial.println("setup initialized");
Serial.begin(9600);
pinMode(rain_pin, INPUT);
pinMode(t_pin, INPUT);
pinMode(ENCODER_A, INPUT_PULLUP);
pinMode(encoderPin_1, INPUT_PULLUP);
pinMode(encoderPin_2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_A), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPin_1), updateEncoder2, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPin_2), updateEncoder2, CHANGE);
sensors.begin();
dht.begin();
if (!bmp.begin()) {
Serial.println("Could not find the BMP180 sensor...");
while (1) {}
}
while (!Serial);
while (!VEML6075.begin()) {
Serial.println("UV sensor failed to start");
delay(8000);
}
Serial.println("All sensors initialized.");
Wire.begin();
//rtc.setTime(22, 21, 0);
startTime = millis();
}
void loop() {
int cMin = myRTC.getMinute();
if(cMin == 00) {
connect();
}
else if(cMin == 10) {
connect();
}
else if(cMin == 20) {
connect();
}
else if(cMin == 30) {
connect();
}
else if(cMin == 40) {
connect();
}
else if(cMin == 50) {
connect();
}
/*float temp_f = getTemperature();
Serial.print("Temperature = ");
Serial.print(temp_f);
Serial.println("F");
float h = getHumidity();
Serial.print("Humidity = ");
Serial.print(h);
Serial.println("%");
float press = getBarometer();
Serial.print("Barometric Pressure = ");
Serial.print(press);
Serial.println(" inHg");
float li = getLight();
Serial.print("Light Intensity = ");
Serial.println(li);
//float rainfall = getRain();
//Serial.print("Total rainfall = ");
//Serial.println(rainfall);
float uva, uvb, uvIndex, power;
getUV(uva, uvb, uvIndex, power);
Serial.print("UVA = ");
Serial.println(uva);
Serial.print("UVB = ");
Serial.println(uvb);
Serial.print("UV Index = ");
Serial.println(uvIndex);
Serial.print("Solar Power = ");
Serial.print(power);
Serial.println(" kW/m2");*/
//float ws = getWind();
/*Serial.print("Wind speed = ");
Serial.print(ws);
Serial.println(" mph");*/
int dir = getDir();/*
Serial.print("Wind direction = ");
Serial.print(dir);
Serial.println(" degrees");*/
//delay(500);
}
void connect() {
float temp_f = getTemperature();
Serial.print("Temperature = ");
Serial.print(temp_f);
Serial.println("F");
float h = getHumidity();
Serial.print("Humidity = ");
Serial.print(h);
Serial.println("%");
float press = getBarometer();
Serial.print("Barometric Pressure = ");
Serial.print(press);
Serial.println(" inHg");
float li = getLight();
Serial.print("Light Intensity = ");
Serial.println(li);
//float rainfall = getRain();
//Serial.print("Total rainfall = ");
//Serial.println(rainfall);
float uva, uvb, uvIndex, power;
getUV(uva, uvb, uvIndex, power);
Serial.print("UVA = ");
Serial.println(uva);
Serial.print("UVB = ");
Serial.println(uvb);
Serial.print("UV Index = ");
Serial.println(uvIndex);
Serial.print("Solar Power = ");
Serial.print(power);
Serial.println(" kW/m2");
float ws = getWind();
Serial.print("Wind speed = ");
Serial.print(ws);
Serial.println(" mph");
int dir = getDir();
Serial.print("Wind direction = ");
Serial.print(dir);
Serial.println(" degrees");
//delay(60000); //delay 1 min
// Create JSON object
StaticJsonDocument<256> doc;
doc["temp"] = temp_f;
doc["humid"] = h;
doc["bar"] = press;
doc["rain"] = rainfall;
doc["uva"] = uva;
doc["uvb"] = uvb;
doc["uvi"] = uvIndex;
doc["power"] = power;
doc["intensity"] = li;
doc["wind"] = ws;
doc["anny"] = dir;
// Serialize and send JSON
String output;
serializeJson(doc, output);
//Serial.println(output);
}
float getTemperature() {
//Serial.println("temp");
sensors.requestTemperatures();
temp_f = sensors.getTempFByIndex(0);
return temp_f;
}
float getHumidity() {
float h = dht.readHumidity();
return isnan(h) ? 0.0 : h;
}
float getBarometer() {
p = bmp.readPressure();
press = (p / 3.2567) / 1000; //change the decimal to match your current conditions - smaller this divsor, the larger your pressure reading will become
return press;
}
float getLight() {
return analogRead(l_pin);
}
float getRain() {
static volatile unsigned int trigger = 0;
static volatile unsigned long lastMicros = 0;
int state = digitalRead(rain_pin);
if (state == LOW && (long)(micros() - lastMicros) >= DEBOUNCE_TIME) {
trigger += 1;
lastMicros = micros();
}
return (trigger * 3.75) / 17; // measured at 1.875mL average per tip, translates to .0738 inches of rain
}
void getUV(float &uva, float &uvb, float &uvIndex, float &power) {
uva = VEML6075.getUva();
uvb = VEML6075.getUvb();
uvIndex = VEML6075.getUvi(uva, uvb);
power = (104 * uvIndex - 18.365) / 1000;
}
float getWind() {
unsigned long currentTime = millis();
if(currentTime - lastTime >= 100) {
long currentTicks = encoderTicks;
long ticksSinceLast = currentTicks - lastTicks;
float rpm = (float)ticksSinceLast / (currentTime - lastTime) * 1000 / 600.0;
float ws = 1.95 * rpm;
Serial.print("wind speed= ");
Serial.println(ws);
lastTime = currentTime;
lastTicks = currentTicks;
}
delay(10000);
}
int getDir() {
int dir = (encoderPosition / 2400) * 360;
Serial.print("Wind direction is ");
Serial.println(dir);
delay(1000);
}
void updateEncoder() {
int a = digitalRead(ENCODER_A);
encoderTicks++;
}
void updateEncoder2() {
static int oldState = 0;
int sig1 = digitalRead(encoderPin_1);
int sig2 = digitalRead(encoderPin_2);
int thisState = sig1 | (sig2 << 1);
const int8_t KNOBDIR[] = {0, 23, 45, 68, 90, 113, 135, 158, 180, 203, 225, 248, 270, 293, 315, 338};
encoderPosition += KNOBDIR[thisState | (oldState << 2)];
if(encoderPosition > 360) {
encoderPosition = encoderPosition - 360;
}
if(encoderPosition < 0) {
encoderPosition = encoderPosition + 360;
}
else {
oldState = thisState;
}
}
This is my entire code snippet...to remove the comment about my code provided being "useless"
Again, I welcome CONTRUCTIVE help in streamlining my code. I do not work with arduinos enough to become as good as many of the trolls on here. I hope this provides more insight into what my end game is. I'd like a weather vane to report a value of 0 to 359...representing which angle the wind is blowing from.
I should note that a mass of my code is commented out. Once I get this portion working the way I like, those lines will be uncommented as they serve to make the whole thing function, send data to a raspberry pi for data logging. Parts are housed in various 3d printed housings and the encoders will be protected by 3d printed caps which allow for freedom of movement but make water saturation very very difficult.