Wgrane na Arduino Sparkfun

This commit is contained in:
Kamil Siejka
2024-11-14 18:50:44 +01:00
parent 6627f545fe
commit f8b20c24bb

View File

@@ -1,4 +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>
#include <Wire.h> #include <Wire.h>
@@ -6,18 +6,17 @@
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#define Version "1.14.0" #define Version "1.14.3"
////2DO: ////2DO:
//pomiar napięcia na baterii
// menu do zmiany zakresu predkosci biegów // menu do zmiany zakresu predkosci biegów
// menu do zmiany zakresu kątów biegów // menu do zmiany zakresu kątów biegów, obwodu koła, ilosci magnesow
#define SCREEN_WIDTH 128 #define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64 #define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define PinInterrupt 0 //on board: RX deklaracja pod stabilizacje TX #define PinInterrupt 0 //on board: RX deklaracja pod stabilizacje TX
#define PinInSpeed 1 //on board: TX #define PinInSpeed 1 //on board: TX
#define ServoSwitch 4 //on board: 4 #define ServoSwitch 4 //on board: 4
#define ServoPin 8 //on board: 8 #define ServoPin 8 //on board: 8
@@ -80,12 +79,6 @@ int servoCurrPos = ServoMaxAngle;
int gearBarHeight = 0; int gearBarHeight = 0;
int gearBarPosition = 0; int gearBarPosition = 0;
float speedForBar = 0; float speedForBar = 0;
//oth
int BrakingLightSwitch;
int ups = 0;
int downs = 0;
int run_hrs = 0;
int run_mins = 0;
//DST //DST
int totalDistMemLocation = 60; int totalDistMemLocation = 60;
int loop_cnt = 0; int loop_cnt = 0;
@@ -101,6 +94,12 @@ float inputVoltage = 0.0;
float measuredVoltage = 0.0; float measuredVoltage = 0.0;
int voltBarHeight = 0; int voltBarHeight = 0;
int voltBarPosition = 0; int voltBarPosition = 0;
//oth
int BrakingLightSwitch;
int ups = 0;
int downs = 0;
int run_hrs = 0;
int run_mins = 0;
void setup() { void setup() {
//SERVO //SERVO
digitalWrite(ServoSwitch, HIGH); digitalWrite(ServoSwitch, HIGH);
@@ -165,7 +164,7 @@ void setup() {
} }
//MEMORY //MEMORY
eeprom_read_block(&totalDistReaded, totalDistMemLocation, 2); eeprom_read_block(&totalDistReaded, totalDistMemLocation, 2);
totalDist = totalDistReaded; totalDist = float(totalDistReaded);
} }
//########################################### LOOP ############################################################ //########################################### LOOP ############################################################
@@ -212,9 +211,9 @@ void loop() {
//duzy font //duzy font
//GEAR //GEAR
display.setCursor(3, 0); display.setCursor(8, 0);
display.println("G:"); display.println("G:");
display.setCursor(35, 0); display.setCursor(40, 0);
display.print(currentGear); display.print(currentGear);
//SPEED_TREND //SPEED_TREND
display.setCursor(100, 00); display.setCursor(100, 00);
@@ -228,9 +227,9 @@ void loop() {
} }
} }
//SPEED //SPEED
display.setCursor(3, 40); display.setCursor(8, 40);
display.println("S:"); display.println("S:");
display.setCursor(35, 40); display.setCursor(40, 40);
display.println(speed, 1); display.println(speed, 1);
//GearBar //GearBar
currentGearRangeMiddle = (currentGearRangeLower + currentGearRangeUpper) / 2; currentGearRangeMiddle = (currentGearRangeLower + currentGearRangeUpper) / 2;
@@ -252,7 +251,7 @@ void loop() {
measuredVoltage = (adcBattVoltValue * referenceVoltage) / maxADCValue; measuredVoltage = (adcBattVoltValue * referenceVoltage) / maxADCValue;
inputVoltage = measuredVoltage * voltageDividerRatio; inputVoltage = measuredVoltage * voltageDividerRatio;
voltBarHeight = int(((inputVoltage-9)/3.6)*64); voltBarHeight = int(((inputVoltage-9)/3)*64); //odjemowanie 9 bo to minimalne napiecie, podział przez 3 bo zakladam max napiecie 12.0V a nie 12.6V
voltBarPosition = 64 - voltBarHeight; voltBarPosition = 64 - voltBarHeight;
display.fillRect(0, voltBarPosition, 1, voltBarHeight, SSD1306_WHITE); display.fillRect(0, voltBarPosition, 1, voltBarHeight, SSD1306_WHITE);
//########################################## ZAPIS DO WYŚWIETLACZA ###################################################### //########################################## ZAPIS DO WYŚWIETLACZA ######################################################
@@ -280,9 +279,10 @@ void loop() {
//TTL DST //TTL DST
// totalDist = 0; // totalDist = 0;
loop_cnt = loop_cnt +1; loop_cnt = loop_cnt +1;
if (loop_cnt >= 20 && totalDistWrited != totalDist ){ if (loop_cnt >= 20 //&& totalDistWrited != totalDist
eeprom_write_block(&totalDist,totalDistMemLocation,2); ){
totalDistWrited = int(totalDist); totalDistWrited = int(totalDist);
// eeprom_write_block(&totalDistWrited,totalDistMemLocation,2);
loop_cnt = 0; loop_cnt = 0;
} }
} }
@@ -430,10 +430,11 @@ void prepareTurnOff() {
myservo.write(servoCurrPos); myservo.write(servoCurrPos);
delay(30); delay(30);
} }
eeprom_write_block(&totalDist,totalDistMemLocation,2); //Na czas diagnostyki
totalDistWrited = totalDist; // eeprom_write_block(&totalDist,totalDistMemLocation,2);
loop_cnt = 0; // totalDistWrited = totalDist;
// loop_cnt = 0;
//
for (; 1500 < (millis() - lastMillisSpd);) { // zmiana z 1000 na 1500 w 1.13.19 for (; 1500 < (millis() - lastMillisSpd);) { // zmiana z 1000 na 1500 w 1.13.19
digitalWrite(ServoSwitch, LOW); digitalWrite(ServoSwitch, LOW);
digitalWrite(ServoPin, LOW); digitalWrite(ServoPin, LOW);