moved to esp32 and added display and counter

This commit is contained in:
Jens Noack 2022-02-18 16:15:08 +01:00
parent 3531ffc9e3
commit 51ad765581
7 changed files with 243 additions and 0 deletions

View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View file

@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View file

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View file

@ -0,0 +1,17 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.1
madhephaestus/ESP32Servo@^0.11.0

View file

@ -0,0 +1,115 @@
#include <ESP32Servo.h>
#include <Arduino.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h" // legacy: #include "SSD1306.h"
SSD1306Wire display(0x3c, SDA, SCL);
Servo ESC; // Der ESC-Controller (Electronic Speed Controller bzw. elektronischer Fahrtregler) wird als Objekt mit dem Namen "ESC" festgelegt.
int Drehregler; // Ausgabewert des Drehreglers
int speed, last_speed; // Das Wort "Geschwindigkeit" steht als Variable für den Ansteuerungswert am ESC.
unsigned long turn = 0;
unsigned long speedturns = 0;
unsigned long speedchanged_ms = 0;
unsigned long speed_u_per_min = 0;
const uint8_t LED_BUILTIN = 2;
const uint8_t POTI_PIN = 4;
const uint8_t ESC_PIN = 16;
const uint8_t LS1_PIN = 14;
const uint8_t MAX_ESC_SPEED = 60;
const uint8_t MIN_ESC_SPEED = 35;
const uint16_t MAX_POTI_VALUE = 4095;
void show_values();
void ISR();
void setup()
{
Serial.begin(9600);
// Initialising the UI will init the display too.
display.init();
display.clear();
display.flipScreenVertically();
//display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_24);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 2, "Init controller");
display.setFont(ArialMT_Plain_10);
display.drawString(64, 40, "You may hear some beeps.");
display.drawString(64, 52, "That's OK! ;-)");
display.display();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
ESC.attach(ESC_PIN,1000,2000);
for(uint8_t i=0;i<=100;i=i+10)
{
display.drawProgressBar(6, 28, 116, 6, i);
delay(500);
ESC.write(0);
display.display();
}
attachInterrupt(LS1_PIN, ISR, RISING);
last_speed = 0;
speed = 0;
}
void loop()
{
Drehregler = analogRead(POTI_PIN); // Dieser Befehl liest den Wert des Potentiometers am analogen Pin A0 aus und speichert ihn unter der Variablen "Drehregler". Der Wert liegt zwischen 0 und 1023.
speed = map(Drehregler, 0, MAX_POTI_VALUE, MIN_ESC_SPEED, MAX_ESC_SPEED); // Der "MAP-" Befehl wandelt den Messwert aus der Variablen "Drehregler" um, damit er am ESC verarbeitet werden kann. Der Zahlenbereich 0 bis 1023 wird dabei in einen Zahlenwert zwischen 0 und 180 umgewandelt.
if(speed != last_speed)
{
last_speed = speed;
speedchanged_ms = millis();
speedturns = 0;
}
if(millis() - speedchanged_ms > 10000)
{
speed_u_per_min = (speedturns*6);
speedturns = 0;
speedchanged_ms = millis();
}
//Serial.print("Drehregler:"); Serial.print(Drehregler); Serial.print(", Speed:");Serial.println(speed);
if(MIN_ESC_SPEED < speed)
ESC.write(speed); // Der endgültige Wert für den ESC wird an den ESC gesendet. Der ESC nimmt das Signal an dieser Stelle auf und steuert den Motor entsprechend der gesendeten Werte an.
else
ESC.write(0);
show_values();
}
void show_values()
{
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(1, 1, "Speed:");
display.drawProgressBar(40, 4, 80, 6, map(speed, MIN_ESC_SPEED,MAX_ESC_SPEED,0,100));
display.setFont(ArialMT_Plain_10);
display.drawString(1, 15, "Turns:");
display.drawString(35, 15, String(turn));
display.setFont(ArialMT_Plain_24);
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(64, 37, String(speed_u_per_min));
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(68,37, "U/min");
display.display();
}
void IRAM_ATTR ISR()
{
turn++;
speedturns++;
}

View file

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html