#include <Arduino.h>
#include "ServoEasing.hpp"
#include <Wire.h>
#include "SparkFunLSM6DS3.h"
#include <NeoPixelBusLg.h>
// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG
#define REMOTEXY__DEBUGLOG_SERIAL Serial
#define REMOTEXY__DEBUGLOG_SPEED 115200
#define LED_PIN 6
#define NUM_LEDS 4
int I2C_SDA = 8;
int I2C_SCL = 10;
LSM6DS3 SensorOne(I2C_MODE, 0x6B);
ServoEasing channel_1;
// Motor Driver signal on pins 0 and 1
int FWD = 0;
int RWD = 1;
int throttle;
int Speed = 0;
const int smoothingWindow = 5;
int ch1Values[smoothingWindow];
int ch2Values[smoothingWindow];
int ch3Values[smoothingWindow];
int ch4Values[smoothingWindow];
int index1 = 0;
bool isBufferFull = false;
float fscale(float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1); // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
} else {
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax) {
return 0;
}
if (invFlag == 0) {
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
} else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}
void smoothValues(int newValue1, int newValue2, int newValue3, int newValue4) {
ch1Values[index1] = newValue1;
ch2Values[index1] = newValue2;
ch3Values[index1] = newValue3;
ch4Values[index1] = newValue4;
index1 = (index1 + 1) % smoothingWindow;
if (index1 == 0) {
isBufferFull = true;
}
}
int calculateAverage(int values[]) {
long sum = 0;
int count = isBufferFull ? smoothingWindow : index1;
for (int i = 0; i < count; i++) {
sum += values[i];
}
return count > 0 ? sum / count : 0;
}
// setting PWM properties
const int freq = 160;
const int ledChannel0 = 4;
const int ledChannel1 = 5;
const int resolution = 8;
const int M1Pin = 0;
const int M2Pin = 1;
void motorSetup() {
ledcSetup(ledChannel0, freq, resolution);
ledcSetup(ledChannel1, freq, resolution);
ledcAttachPin(M1Pin, ledChannel0);
ledcAttachPin(M2Pin, ledChannel1);
}
#define colorSaturation 255
NeoPixelBusLg<NeoGrbFeature, NeoWs2812xMethod> strip(4, 6);
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);
bool toggleLight = true;
bool toggleSquere = false;
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP32CORE_WIFI_POINT
#include <WiFi.h>
// LilaChevy, GelberGTO, GrunerGTO, DodgeCharger
// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "Corrado"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377
#include <RemoteXY.h>
// RemoteXY GUI configuration
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 74 bytes
{ 255, 5, 0, 0, 0, 67, 0, 17, 0, 0, 0, 228, 1, 200, 84, 1, 1, 5, 0, 1,
92, 2, 14, 14, 0, 208, 24, 76, 105, 99, 104, 116, 0, 2, 85, 60, 27, 10, 0, 193,
26, 31, 31, 79, 78, 0, 79, 70, 70, 0, 4, 20, 18, 21, 62, 48, 22, 26, 4, 128,
37, 66, 22, 176, 22, 26, 4, 65, 38, 68, 12, 160, 204, 26 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t button_01; // =1 if button pressed, else =0
uint8_t switch_01; // =1 if switch ON and =0 if OFF
int8_t slider_01; // from -100 to 100
int8_t slider_02; // from -100 to 100
int8_t slider_03; // from -100 to 100
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
void setup() {
RemoteXY_Init();
Serial.begin(115200);
channel_1.attach(3);
pinMode(FWD, OUTPUT);
pinMode(RWD, OUTPUT);
motorSetup();
SensorOne.begin();
strip.Begin();
strip.SetLuminance(128);
strip.SetPixelColor(0, blue);
strip.SetPixelColor(1, blue);
strip.SetPixelColor(2, blue);
strip.SetPixelColor(3, blue);
strip.Show();
delay(1000);
strip.SetPixelColor(0, red);
strip.SetPixelColor(1, white);
strip.SetPixelColor(2, white);
strip.SetPixelColor(3, red);
strip.Show();
}
unsigned long lastRecvTime = 0;
long timer1, timer2, timer3 = 0;
int lightSelect = 1;
int lightOptions = 6;
bool extraSteer = true;
int previousValue1 = 0;
unsigned long lastTimeNeutral = 0;
bool isAdjusting = false;
int value1 = 0;
void loop() {
RemoteXY_Handler();
if (millis() - timer3 > 200) {
extraSteer = !extraSteer;
Serial.println(value1);
timer3 = millis();
}
if (RemoteXY.connect_flag) {
if (millis() - timer2 > 1) {
int maxSpeed = 128;
int slider_01Value = 0;
if (RemoteXY.switch_01) slider_01Value = -RemoteXY.slider_01;
else slider_01Value = RemoteXY.slider_01;
if (slider_01Value > 0) {
int y_axis = map(slider_01Value, 5, 100, 0, maxSpeed);
ledcWrite(ledChannel0, y_axis);
ledcWrite(ledChannel1, 0);
} else {
int y_axis = map(slider_01Value, -5, -100, 0, maxSpeed);
ledcWrite(ledChannel0, 0);
ledcWrite(ledChannel1, y_axis);
}
if (RemoteXY.slider_01 == 0) {
ledcWrite(ledChannel0, 0);
ledcWrite(ledChannel1, 0);
}
value1 = RemoteXY.slider_02 + RemoteXY.slider_03;
//if(value1 > 100) value1 = 100;
//if(value1 < -100) value1 = -100;
//int steeringValue = map(value1, 100, -100, 1250, 1800);
// Überwache den Wert von value1
if (RemoteXY.slider_02 == 0 && previousValue1 != 0) {
// Wenn value1 gerade auf 0 zurückgekehrt ist
if (!isAdjusting) {
isAdjusting = true;
lastTimeNeutral = millis();
}
}
if (isAdjusting && millis() - lastTimeNeutral >= 100) {
// Wenn die 100 ms vorbei sind, setze value1 zurück auf 0
//value1 = 0;
isAdjusting = false;
}
/*
if (extraSteer) {
value1 = value1 + 10;
} else value1 = value1 - 10;
*/
if (isAdjusting) {
if (previousValue1 > 0) {
value1 = value1 - 40;
} else {
value1 = value1 + 40;
}
Serial.println(value1);
}
int steeringValue = map(value1, 100, -100, 1200, 1800); // 1000 2000
channel_1.writeMicroseconds(steeringValue);
if (!isAdjusting) previousValue1 = RemoteXY.slider_02;
}
if (millis() - timer1 > 100) {
if (RemoteXY.button_01 == 1 && !toggleSquere) {
toggleSquere = true;
lightSelect++;
if (lightSelect > lightOptions) lightSelect = 0;
}
if (RemoteXY.button_01 == 0 && toggleSquere) {
toggleSquere = false;
}
if (lightSelect > 0) {
if (lightSelect == 1) {
strip.SetPixelColor(0, red);
strip.SetPixelColor(1, white);
strip.SetPixelColor(2, white);
strip.SetPixelColor(3, red);
}
if (lightSelect == 2) {
strip.SetPixelColor(0, blue);
strip.SetPixelColor(1, blue);
strip.SetPixelColor(2, blue);
strip.SetPixelColor(3, blue);
}
if (lightSelect == 3) {
strip.SetPixelColor(0, green);
strip.SetPixelColor(1, green);
strip.SetPixelColor(2, green);
strip.SetPixelColor(3, green);
}
if (lightSelect == 4) {
strip.SetPixelColor(0, red);
strip.SetPixelColor(1, red);
strip.SetPixelColor(2, red);
strip.SetPixelColor(3, red);
}
if (lightSelect == 5) {
RgbColor orange(255, 165, 0);
strip.SetPixelColor(0, orange);
strip.SetPixelColor(1, orange);
strip.SetPixelColor(2, orange);
strip.SetPixelColor(3, orange);
}
if (lightSelect == 6) {
RgbColor lila(153, 50, 204);
strip.SetPixelColor(0, lila);
strip.SetPixelColor(1, lila);
strip.SetPixelColor(2, lila);
strip.SetPixelColor(3, lila);
}
strip.SetLuminance(128);
//Serial.println("light on");
} else {
//strip.SetLuminance(0);
strip.SetPixelColor(0, black);
strip.SetPixelColor(1, black);
strip.SetPixelColor(2, black);
strip.SetPixelColor(3, black);
Serial.println("light off");
}
strip.Show();
timer1 = millis();
}
} else {
ledcWrite(ledChannel0, 0);
ledcWrite(ledChannel1, 0);
}
}