-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoto_visor_arduino.ino
190 lines (152 loc) · 3.61 KB
/
moto_visor_arduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define i2c_Address 0x3c
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
const int buttonPin = 4;
const int ledPin = 2;
int buttonState = 0;
String message = "";
int buttonPushCounter = 0; // variable pour le comptage du nombre d'appuis sur le bouton poussoir
int funcState = 0;
const int BATTERYPIN = A0; //pin de la batterie
const float TensionMin = 3.2; //tension min
const float TensionMax = 4.2; //tension max
String battery ="";
boolean cmd = 0;
String dir ="";
String speed ="";
void setup() {
display.begin( i2c_Address, true );
display.setRotation(1);
display.clearDisplay();
display.display();
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
if(!cmd){
while(Serial.available() > 0){
message = Serial.readString();
cmd = true;
// Serial.println("----------------------------");
Serial.println(message);
}
}
if(cmd){
if(message != "dir" and message !="speed"){
cmd = false;
// Serial.println("0000000000000000000");
message ="";
}
if(message == "speed") {
while(Serial.available() > 0){
speed = Serial.readString();
// Serial.println("+++++++++++++++");
Serial.println(speed);
cmd = false;
message ="";
}
}
if(message == "dir"){
while(Serial.available() > 0){
dir = Serial.readString();
// Serial.println("iiiiiiiiiiiiiiii");
cmd = false;
message ="";
}
}
}
setMode();
getBattery();
buttonPressed();
}
enum fcnMode {
OFF,
MOD1,
MOD2,
MOD3,
NBSTATE
}; // OFF = 0 and NBSTATE=4
void buttonPressed(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
buttonPushCounter++;
funcState = buttonPushCounter % NBSTATE;
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
delay(400);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
void setMode() {
switch (funcState) {
case OFF:
mode4();
break;
case MOD1:
mode1();
break;
case MOD2:
mode2();
break;
case MOD3:
mode3();
break;
}
}
void mode1() {
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.clearDisplay();
display.print(speed);
display.display();
}
void mode2() {
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.clearDisplay();
display.print(dir);
display.display();
}
void mode3() {
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.clearDisplay();
display.print("batterie :" + battery);
display.display();
}
void mode4() {
display.clearDisplay();
display.display();
}
int getBattery ()
{
float b = analogRead(BATTERYPIN); //valeur analogique
int minValue = (1023 * TensionMin) / 5; //Arduino
int maxValue = (1023 * TensionMax) / 5; //Arduino
b = ((b - minValue) / (maxValue - minValue)) * 100; //mettre en pourcentage
if (b > 100) //max is 100%
b = 100;
else if (b < 0) //min is 0%
b = 0;
int valeur = b;
battery = b;
return b;
}