//Parametros leituras_botoes
int joystick_y, joystick_x; //valores joystick
int joystick_y_pin = 2; //pino joy y
int joystick_x_pin = 1; //pino joy x
bool button_c, button_z; //estados botoes
int button_c_pin = 8; //pino bot c
int button_z_pin = 7; //pino bot z
//Parametros estado_bateria
int n_medicoes = 30;
int fator_cor = 97.0;
int bat_mv; //valores bateria
int bat_pct; //valores bateria
int bat_pin = 3; //pino bateria
int bat_full = 4200; //tensao cheia
int bat_empty = 3600; //tensao vazio
int k;
bool estado_bateria1 = 0;
bool estado_bateria2 = 0;
bool estado_bateria3 = 0;
//Parametros LED
int led_pin = 6; //pino led
int time = 0;
int periode20 = 2000;
int periode10 = 400;
int ledState = 0;
int previoustime = 0;
int power_on = 1;
int time_on = 600;
//Parametros radio
#include <SPI.h>
#include "RF24.h"
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);
boolean sendOK = false;
struct remotePackage {
int valXJoy;
int valYJoy;
boolean valCbutton;
boolean valZbutton;
} ;
remotePackage remPack;
void setup() {
//configura pinos dos botoes como input
pinMode(button_c_pin, INPUT);
pinMode(button_z_pin, INPUT);
//ativa o resistor pull-up interno
digitalWrite(button_c_pin, HIGH);
digitalWrite(button_z_pin, HIGH);
//Configura LED
pinMode(led_pin, OUTPUT);
//configura radio
Serial.begin(115200);
radio.begin();
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.openWritingPipe(pipe);
}
void loop() {
leituras_botoes(); //Chama a função de leitura dos botoes
estado_bateria(); //Chama a função de estado da bateria
controle_led(); //Chama a função de controle do led
radio_controle(); //Chama a função de controle do rádio
debug(); //Chama a função de debug
}
void leituras_botoes() {
//realiza leitura do joy e but
joystick_y = analogRead(joystick_y_pin);
joystick_x = analogRead(joystick_x_pin);
button_c = digitalRead(button_c_pin);
button_z = digitalRead(button_z_pin);
}
void estado_bateria() {
//Le a tensão da bateria e calcula a porcentagem equivalente
bat_mv = 0;
for ( k = 0 ; k <= n_medicoes; k++ ) {
bat_mv = analogRead(bat_pin) + bat_mv;
}
bat_mv = (bat_mv / n_medicoes) * (5000.0 / 1023.0) * (fator_cor / 100.0);
bat_pct = map(bat_mv, bat_empty, bat_full, 0, 100);
}
void controle_led() {
if (power_on == 1) {
time = millis();
previoustime = time;
while (ledState < 61) {
time = millis();
if (time - previoustime >= 10) {
ledState = ledState + 1;
previoustime = time;
}
analogWrite(led_pin, ledState);
}
delay(500);
if (bat_pct >= 80 ) {
time = millis();
while (time < 5 * time_on) {
time = millis();
analogWrite(led_pin, 31 + 30 * cos(2 * PI / time_on * time));
}
}
if (bat_pct < 80 && bat_pct >= 60 ) {
time = millis();
while (time < 4 * time_on) {
time = millis();
analogWrite(led_pin, 31 + 30 * cos(2 * PI / time_on * time));
}
}
if (bat_pct < 60 && bat_pct >= 40 ) {
time = millis();
while (time < 3 * time_on) {
time = millis();
analogWrite(led_pin, 31 + 30 * cos(2 * PI / time_on * time));
}
}
if (bat_pct < 40 && bat_pct >= 20 ) {
time = millis();
while (time < 2 * time_on) {
time = millis();
analogWrite(led_pin, 31 + 30 * cos(2 * PI / time_on * time));
}
}
power_on = 0;
}
if (bat_pct < 20) {
estado_bateria1 = 1;
}
if (bat_pct < 10) {
estado_bateria2 = 1;
}
if (bat_pct < 2) {
estado_bateria3 = 1;
}
if (bat_pct > 30) {
estado_bateria1 = 0;
estado_bateria2 = 0;
estado_bateria3 = 0;
}
if (estado_bateria1 == 0) {
analogWrite(led_pin, 61);
}
else if (estado_bateria3 == 1) {
time = millis();
if (time - previoustime >= periode10) {
previoustime = time;
if (ledState >= 61) {
ledState = 0;
} else {
ledState >= 61;
}
previoustime = time;
}
analogWrite(led_pin, ledState);
}
else if (estado_bateria1 == 1 && estado_bateria2 == 1) {
time = millis();
analogWrite(led_pin, 31 + 30 * cos(2 * PI / periode10 * time));
}
else if (estado_bateria1 == 1 && estado_bateria2 == 0) {
time = millis();
analogWrite(led_pin, 31 + 30 * cos(2 * PI / periode20 * time));
}
}
void radio_controle() {
remPack.valXJoy = map(joystick_x, 0, 1023, 0, 255);
remPack.valYJoy = map(joystick_y, 0, 1023, 0, 255);
remPack.valCbutton = !button_c;
remPack.valZbutton = !button_z;
//send data via radio to RX
sendOK = radio.write( &remPack, sizeof(remPack) );
}
void debug() {
Serial.print("Tensão Bateria: ");
Serial.print(bat_mv);
Serial.print(" mV, Porcentagem Bateria: ");
Serial.print(bat_pct);
Serial.print(" %, Joystick X: ");
Serial.print(joystick_x);
Serial.print(", Joystick Y: ");
Serial.print(joystick_y);
Serial.print(", Botão C: ");
Serial.print(button_c);
Serial.print(", Botão Z: ");
Serial.print(button_z);
Serial.print(" ");
Serial.print(estado_bateria1);
Serial.print(" ");
Serial.print(estado_bateria2);
Serial.print(" ");
Serial.print(estado_bateria3);
Serial.print('\n');
}