Created
February 26, 2023 07:49
-
-
Save reefwing/2c5d000ad39ea6d17e1eb6504d0ec8e8 to your computer and use it in GitHub Desktop.
Code stub for Nano ESC
This file contains hidden or 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 <Arduino.h> | |
#include <U8g2lib.h> | |
#include <SPI.h> | |
/****************************************************************** | |
* Nano PIN MAPPING | |
******************************************************************/ | |
#define MOSI 11 | |
#define DC 1 | |
#define SCK 13 | |
#define CS A0 | |
#define VBAT A5 | |
#define VPOT A7 | |
/****************************************************************** | |
* BATTERY MONITORING | |
******************************************************************/ | |
#define R1 16000 // 16K Resistor | |
#define R2 10000 // 10K Resistor | |
#define DIV_RATIO (float)(R1 + R2) / (float)R2 | |
#define VREF 5.0 | |
/****************************************************************** | |
* GENERAL | |
******************************************************************/ | |
#define DECIMAL 10 // itoa - RADIX option | |
#define SAMPLE_TIME 1000 // 1 second = 1000 ms | |
/****************************************************************** | |
* GLOBAL VARIABLES | |
******************************************************************/ | |
U8X8_SSD1306_128X64_NONAME_4W_HW_SPI u8x8(CS, DC); // Hardware SPI | |
float batteryVoltage = 0.0; | |
char freqAsChar[6], throttleAsChar[6], vBatAsChar[6]; | |
bool toggle = false; | |
int loopCounter = 0, loopFrequency = 0; | |
unsigned long startTime, currentTime; | |
/****************************************************************** | |
* SETUP | |
******************************************************************/ | |
void setup() { | |
u8x8.begin(); | |
u8x8.setFont(u8x8_font_chroma48medium8_r); | |
u8x8.drawString(4, 2, "NANO ESC"); | |
startTime = millis(); | |
} | |
/****************************************************************** | |
* ESC LOOP | |
******************************************************************/ | |
void loop() { | |
currentTime = millis(); | |
loopCounter++; | |
// Read throttle pot position and convert to percentage | |
float pot = (((float)analogRead(VPOT) + 0.5) / 1024.0) * 100.0; | |
itoa((int)pot, throttleAsChar, DECIMAL); // itoa = Int to ASCII | |
u8x8.drawString(1, 4, "THROTTLE: "); | |
u8x8.drawString(10, 4, throttleAsChar); | |
u8x8.drawString(14, 4, "%"); | |
if (toggle) { | |
u8x8.drawString(15, 1, "*"); | |
u8x8.drawString(0, 6, "LOOP FREQ: "); | |
itoa(loopFrequency, freqAsChar, DECIMAL); | |
u8x8.drawString(10, 6, freqAsChar); | |
u8x8.drawString(13, 6, " Hz"); | |
} | |
else { | |
u8x8.drawString(15, 1, " "); | |
u8x8.drawString(0, 6, "LiPo VOLT: "); | |
dtostrf(batteryVoltage, 4, 1, vBatAsChar); //4 is mininum width, 1 is precision | |
u8x8.drawString(10, 6, vBatAsChar); | |
u8x8.drawString(14, 6, "V "); | |
} | |
if (currentTime - startTime >= SAMPLE_TIME) { | |
// Check Battery Voltage every second | |
batteryVoltage = (((float)analogRead(VBAT) + 0.5) / 1024.0) * DIV_RATIO * VREF; | |
toggle = !toggle; | |
loopFrequency = loopCounter; | |
loopCounter = 0; | |
startTime = currentTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment