Dodanie battery bar, zmiana podejscia do wygaszania

This commit is contained in:
Kamil Siejka
2024-10-14 21:25:11 +02:00
parent 2ff2aed114
commit 8bafa3cb08
3 changed files with 56 additions and 33 deletions

View File

@@ -1,3 +1,4 @@
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include <Servo.h> // model servo: DS3218 PRO #include <Servo.h> // model servo: DS3218 PRO
#include <SPI.h> #include <SPI.h>
@@ -6,8 +7,11 @@
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#define Version "1.12.12" #define Version "1.13.17"
////2DO: ////2DO:
//pomiar napięcia na baterii
// menu do zmiany zakresu predkosci biegów
// menu do zmiany zakresu kątów biegów
#define SCREEN_WIDTH 128 #define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64 #define SCREEN_HEIGHT 64
@@ -20,8 +24,8 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define ServoPin 8 //on board: 8 #define ServoPin 8 //on board: 8
#define BrakingLight 9 //on board: 9 BrakingLight #define BrakingLight 9 //on board: 9 BrakingLight
#define PinLED 10 //on board: 10 ORANGE loop signal #define PinLED 10 //on board: 10 ORANGE loop signal
#define Btn1 18 //on board: A0 Button0 #define Btn1 18 //on board: A0 Button1
#define Btn2 19 //on board: A1 Button1 #define VoltInptPin 19 //on board: A1 Battery Voltage
#define ServoMaxAngle 130 #define ServoMaxAngle 130
#define MaxAngle 179 #define MaxAngle 179
@@ -32,7 +36,7 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define MagnetsCnt 4 #define MagnetsCnt 4
#define ms2kmh 3.6 #define ms2kmh 3.6
#define Pi 3.1416 #define Pi 3.1416
#define TimeToSleep 180000 //3 min #define TimeToSleep 5000 //5 sec
Servo myservo; Servo myservo;
@@ -89,6 +93,14 @@ int loop_cnt = 0;
unsigned int totalDist = 0; unsigned int totalDist = 0;
unsigned int totalDistReaded = 0; unsigned int totalDistReaded = 0;
unsigned int totalDistWrited = 0; unsigned int totalDistWrited = 0;
//BATTERY
float referenceVoltage = 5.1;
int maxADCValue = 1023;
float voltageDividerRatio = 3.0;
int adcBattVoltValue = 0;
float inputVoltage = 0.0;
float measuredVoltage = 0.0;
void setup() { void setup() {
//SERVO //SERVO
@@ -119,6 +131,7 @@ void setup() {
pinMode(PinInterrupt, OUTPUT); pinMode(PinInterrupt, OUTPUT);
digitalWrite(PinInterrupt, HIGH); digitalWrite(PinInterrupt, HIGH);
pinMode(Btn1, INPUT); pinMode(Btn1, INPUT);
pinMode(VoltInptPin, INPUT);
//OUTPUT //OUTPUT
pinMode(PinLED, OUTPUT); pinMode(PinLED, OUTPUT);
pinMode(BrakingLight, OUTPUT); pinMode(BrakingLight, OUTPUT);
@@ -161,6 +174,16 @@ void loop() {
loopTime = millis(); loopTime = millis();
display.clearDisplay(); display.clearDisplay();
// //DIAG Btn1
// display.setTextSize(1);
// display.setCursor(0, 30);
// display.println("Btn1:");
// display.setCursor(30, 20);
// display.println(digitalRead(Btn1));
// display.setCursor(30, 30);
// display.println(analogRead(Btn1));
// display.setCursor(55, 30);
display.setTextSize(3); display.setTextSize(3);
//################################################ //################################################
@@ -171,11 +194,15 @@ void loop() {
speed_last = 0.0; speed_last = 0.0;
speed_last_2 = 0.0; speed_last_2 = 0.0;
speed_last_3 = 0.0; speed_last_3 = 0.0;
if (sleepSpd >= TimeToSleep) { //ustaw bieg 8. jeśli nie było odcztu od 120 s //przejście w tryb uśpienia za pomocą odpowiedniego ustawienia koła i magnesu
prepareTurnOff(); if (digitalRead(PinInSpeed) == LOW) {
display.fillCircle(75, 10, 10, SSD1306_WHITE);
if (sleepSpd >= TimeToSleep) {
prepareTurnOff();
}
}
} }
}
//DIAG
if (speed > 99 || isinf(speed)) { if (speed > 99 || isinf(speed)) {
speed = 21.37; speed = 21.37;
} }
@@ -206,25 +233,26 @@ void loop() {
display.setCursor(35, 40); display.setCursor(35, 40);
display.println(speed, 1); display.println(speed, 1);
//GearBar //GearBar
if (sleepSpd <= 30000) { currentGearRangeMiddle = (currentGearRangeLower + currentGearRangeUpper) / 2;
currentGearRangeMiddle = (currentGearRangeLower + currentGearRangeUpper) / 2; display.fillRect(115, 32, 7, 1, SSD1306_WHITE);
display.fillRect(115, 32, 7, 1, SSD1306_WHITE); display.fillRect(115, 0, 7, 1, SSD1306_WHITE);
display.fillRect(115, 0, 7, 1, SSD1306_WHITE); display.fillRect(115, 63, 7, 1, SSD1306_WHITE);
display.fillRect(115, 63, 7, 1, SSD1306_WHITE);
if (speed >= currentGearRangeMiddle) {
if (speed >= currentGearRangeMiddle) { gearBarHeight = int(((speedForBar - currentGearRangeMiddle) / (currentGearRangeUpper - currentGearRangeMiddle)) * 32);
gearBarHeight = int(((speedForBar - currentGearRangeMiddle) / (currentGearRangeUpper - currentGearRangeMiddle)) * 32); gearBarPosition = 32 - gearBarHeight;
gearBarPosition = 32 - gearBarHeight; display.fillRect(122, gearBarPosition, 4, gearBarHeight, SSD1306_WHITE);
display.fillRect(122, gearBarPosition, 4, gearBarHeight, SSD1306_WHITE);
} else {
gearBarHeight = int(((currentGearRangeMiddle - speedForBar) / (currentGearRangeMiddle - currentGearRangeLower)) * 32);
display.fillRect(122, 32, 4, gearBarHeight, SSD1306_WHITE);
}
} else { } else {
display.setTextSize(2); gearBarHeight = int(((currentGearRangeMiddle - speedForBar) / (currentGearRangeMiddle - currentGearRangeLower)) * 32);
display.setCursor(90, 25); display.fillRect(122, 32, 4, gearBarHeight, SSD1306_WHITE);
display.println(int(((TimeToSleep - sleepSpd) / 1000)));
} }
//VOLT_BAR
adcBattVoltValue = analogRead(VoltInptPin);
measuredVoltage = (adcBattVoltValue * referenceVoltage) / maxADCValue;
inputVoltage = measuredVoltage * voltageDividerRatio;
display.fillRect(0, 63, ((inputVoltage-9)/3.6)*124, 1 , SSD1306_WHITE);
//########################################## ZAPIS DO WYŚWIETLACZA ######################################################
display.display(); display.display();
loopTime = millis(); loopTime = millis();
@@ -233,10 +261,7 @@ void loop() {
delay(10); delay(10);
} }
////przejście w tryb uśpienia za pomocą przycisku
if (digitalRead(Btn1) == HIGH) {
prepareTurnOff();
}
if (speedTrend == -1 && speed > 0.0 ) { if (speedTrend == -1 && speed > 0.0 ) {
if (BrakingLightSwitch == 1) { if (BrakingLightSwitch == 1) {
@@ -256,9 +281,7 @@ void loop() {
eeprom_write_block(&totalDist,totalDistMemLocation,2); eeprom_write_block(&totalDist,totalDistMemLocation,2);
totalDistWrited = totalDist; totalDistWrited = totalDist;
loop_cnt = 0; loop_cnt = 0;
} }
} }
void calcSpeed() { void calcSpeed() {
@@ -407,7 +430,7 @@ void prepareTurnOff() {
totalDistWrited = totalDist; totalDistWrited = totalDist;
loop_cnt = 0; loop_cnt = 0;
for (; 1000 < (millis() - lastMillisSpd);) { for (; 500 < (millis() - lastMillisSpd);) { // zmiana z 1000 na 500 w 1.13.X
digitalWrite(ServoSwitch, LOW); digitalWrite(ServoSwitch, LOW);
digitalWrite(ServoPin, LOW); digitalWrite(ServoPin, LOW);
//INFO //INFO

Binary file not shown.