Hi guys, I have a problem with this mentioned scheme.
When I try to calibrate the analog signal (A0 and A1) with the Teensy 3.6 it works correctly but with the 4.1 it doesn't.
Compared to the video in the link (video) which is working with teensy 3.6 the LED switching times are much quicker
Furthermore, teensy 4.1 does not send information to the connected Nextion monitor.
I have only this problem the other function: button, pov, rotary and enter and out to configuration mode are ok
Does anyone have any advice for me on this.
//POV
#include <Bounce.h>
int PinButton0 = 31; // COMM
int PinButton1 = 29; // A- Top
int PinButton2 = 30; //B- Right
int PinButton3 = 27; //C-Left
int PinButton4 = 28; //X-bottom
Bounce button0 = Bounce(PinButton0, 10);
Bounce button1 = Bounce(PinButton1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(PinButton2, 10); // which is appropriate for
Bounce button3 = Bounce(PinButton3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(PinButton4, 10);
//--End
//Clutches
#define CLUTCHLH A1
#define CLUTCHRH A0
#define WINDOW_SIZE 50
//Upload
#define HWSERIAL Serial1
void UploadNextionHMI() {
int incomingByte;
if (Serial.available() > 0) {
incomingByte = Serial.read();
HWSERIAL.write(incomingByte);
// put your main code here, to run repeatedly:
}
if (HWSERIAL.available()>0){
incomingByte = HWSERIAL.read();
Serial.write(incomingByte);
}
}
//
int INDEXLH = 0;
int VALUELH = 0;
int SUMLH = 0;
int READINGSLH[WINDOW_SIZE];
int AVERAGEDLH = 0;
int AVERAGEDLHBEFORE = 0;
int AVERAGEDLHMAPPED = 0;
int INDEXRH = 0;
int VALUERH = 0;
int SUMRH = 0;
int READINGSRH[WINDOW_SIZE];
int AVERAGEDRH = 0;
int AVERAGEDRHBEFORE = 0;
int AVERAGEDRHMAPPED = 0;
int AVERAGEDCLUTCH = 0;
int CLUTCH = 0;
//
int MAXLH = 1023;
int MINLH = 1023;
int MAXRH = 1023;
int MINRH = 1023;
int CLUTCHRANGELH;
int CLUTCHRANGERH;
int CLUTCHRANGE;
//
//EG added 21/03/21- Clutch Calibration
#include <EEPROM.h>
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
int currentButtonState;
int configMode = 0;
int lastButtonState = HIGH;
int pressStarted=0;
float BITINGPOINT;
int BITINGPOINTFOREEPROM = 50;
//Used For saving
int button6;
int button10;
//
//Used for Clutch calibration
int button7;
int button8;
//
int isConfigAlreadyBeenSaved = 0;
int isMaxCalibrationBeenDone = 0;
int isMinCalibrationBeenDone = 0;
int MAXLHaddress = 0;
int MAXRHaddress = 1;
int MINLHaddress = 2;
int MINRHaddress = 3;
int BITINGPOINTaddress = 4;
//EG 07202021 - Clutches preset
int preset01 =35;
int preset02 =45;
int preset03 =55;
int preset04 =65;
int BITINGPOINT01address = 5;
int BITINGPOINT02address = 6;
int BITINGPOINT03address = 7;
int BITINGPOINT04address = 8;
int selectedPresetAddress = 9;
int buttonClutchPreset = 0;
int selectedPreset = 0;
//End
//Upload Mode
unsigned long UppressedTime = 0;
unsigned long UpreleasedTime = 0;
int UpcurrentButtonState;
int UplastButtonState = HIGH;
int UppressStarted=0;
int UploadingMode =0;
//End
// ----- LEDS ---------
// Uncomment to enable leds
#define INCLUDE_WS2812B
// How many leds
#define WS2812B_RGBLEDCOUNT 18
// Data pin
#define WS2812B_DATAPIN 33
// 0 left to right, 1 right to left
#define WS2812B_RIGHTTOLEFT 0
//From AMS studio
#define ENABLE_PULLUPS
#define NUMROTARIESHALF 3
#define NUMROTARIESFULL 4
struct rotariesdefHalf{
byte pin1;
byte pin2;
int ccwchar;
int cwchar;
volatile unsigned char state;
};
struct rotariesdefFull{
byte pin1;
byte pin2;
int ccwchar;
int cwchar;
volatile unsigned char state;
};
rotariesdefHalf rotariesHalf[NUMROTARIESHALF]{
{6,7,19,20,0},
{8,9,21,22,0},
{10,11,23,24,0},
//{2,3,14,15,0},
};
rotariesdefFull rotariesFull[NUMROTARIESFULL]{
//{4,5,28,29,0},
{2,3,25,26,0},
{4,5,27,28,0},
{25,26,29,30,0},
{12,24,31,32,0},
};
#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char ttableHalf[6][4] = {
// R_START (00)
{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CCW_BEGIN
{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
// R_CW_BEGIN
{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
// R_START_M (11)
{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
// R_CW_BEGIN_M
{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
// R_CCW_BEGIN_M
{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
};
#define R_CW_FINAL 0x1
#define R_CW_BEGINFULL 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGINFULL 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
const unsigned char ttableFull[7][4] = {
// R_START
{R_START, R_CW_BEGINFULL, R_CCW_BEGINFULL, R_START},
// R_CW_FINAL
{R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
// R_CW_BEGIN
{R_CW_NEXT, R_CW_BEGINFULL, R_START, R_START},
// R_CW_NEXT
{R_CW_NEXT, R_CW_BEGINFULL, R_CW_FINAL, R_START},
// R_CCW_BEGIN
{R_CCW_NEXT, R_START, R_CCW_BEGINFULL, R_START},
// R_CCW_FINAL
{R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
// R_CCW_NEXT
{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGINFULL, R_START},
};
//End by EG
#include <Adafruit_NeoPixel.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
byte rowPins[ROWS] = {16, 18, 19, 20}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {23,22,21}; //connect to the column pinouts of the keypad
char keyMap[ROWS][COLS] = {
{2,3,4},
{5,6,7},
{8,9,10},
{11,12,13}
};
Keypad buttbx = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
void WriteToComputer() {
while (HWSERIAL.available()) {
char c = (char)HWSERIAL.read();
Serial.write(c);
}
}
#ifdef INCLUDE_WS2812B
Adafruit_NeoPixel WS2812B_strip = Adafruit_NeoPixel(WS2812B_RGBLEDCOUNT, WS2812B_DATAPIN, NEO_RGB + NEO_KHZ400);
bool LedsDisabled = false;
byte r, g, b;
void ReadLeds() {
while (!Serial.available()) {}
WS2812B_strip.setBrightness(Serial.read());
for (int i = 0; i < WS2812B_RGBLEDCOUNT; i++) {
while (!Serial.available()) {}
r = Serial.read();
while (!Serial.available()) {}
g = Serial.read();
while (!Serial.available()) {}
b = Serial.read();
if (WS2812B_RIGHTTOLEFT > 0)
WS2812B_strip.setPixelColor(WS2812B_RGBLEDCOUNT - 1 - i, r, g, b);
else {
WS2812B_strip.setPixelColor(i, r, g, b);
}
WriteToComputer();
}
WS2812B_strip.show();
for (int i = 0; i < 3; i++) {
while (!Serial.available()) {}
Serial.read();
}
}
void DisableLeds() {
LedsDisabled = true;
Serial.write("Leds disabled");
}
#endif
static long baud = 921600;
static long newBaud = baud;
void lineCodingEvent(long baud, byte databits, byte parity, byte charFormat)
{
newBaud = baud;
}
void setup() {
//EEPROM.write(MAXLHaddress,MAXLH/4);
//EEPROM.write(MAXRHaddress,MAXRH/4);
//EEPROM.write(MINLHaddress,MINLH/4);
//EEPROM.write(MINRHaddress,MINRH/4);
//EEPROM.write(BITINGPOINTaddress,BITINGPOINTFOREEPROM);
MAXLH = EEPROM.read(MAXLHaddress)*4;
MAXRH = EEPROM.read(MAXRHaddress)*4;
MINLH = EEPROM.read(MINLHaddress)*4;
MINRH = EEPROM.read(MINRHaddress)*4;
//EG 07202021
preset01 = EEPROM.read(BITINGPOINT01address);
preset02 = EEPROM.read(BITINGPOINT02address);
preset03 = EEPROM.read(BITINGPOINT03address);
preset04 = EEPROM.read(BITINGPOINT04address);
selectedPreset = EEPROM.read(selectedPresetAddress);
//End
BITINGPOINT = EEPROM.read(BITINGPOINTaddress);
CLUTCHRANGELH = (MAXLH-MINLH);
CLUTCHRANGERH = (MAXRH-MINRH);
CLUTCHRANGE = CLUTCHRANGELH +CLUTCHRANGERH;
pinMode(CLUTCHLH, INPUT_PULLUP);
pinMode(CLUTCHRH, INPUT_PULLUP);
pinMode(PinButton0, INPUT_PULLUP);
pinMode(PinButton1, INPUT_PULLUP);
pinMode(PinButton2, INPUT_PULLUP);
pinMode(PinButton3, INPUT_PULLUP);
pinMode(PinButton4, INPUT_PULLUP);
pinMode(17,INPUT_PULLUP);
pinMode(18,INPUT_PULLUP);
pinMode(19,INPUT_PULLUP);
pinMode(20,INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(baud);
HWSERIAL.begin(baud);
#ifdef INCLUDE_WS2812B
WS2812B_strip.begin();
WS2812B_strip.setPixelColor(0, 0, 0, 0);
WS2812B_strip.show();
#endif
rotary_initHalf();
rotary_initFull();
//Joystick.useManualSend(true);
//EG 07212021
if (selectedPreset == 0){
Serial1.print(F("clutch_m.txt=\"Map 1\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset01);
printEnd();
}
if (selectedPreset == 1){
Serial1.print(F("clutch_m.txt=\"Map 2\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset02);
printEnd();
}
if (selectedPreset == 2){
Serial1.print(F("clutch_m.txt=\"Map 3\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset03);
printEnd();
}
if (selectedPreset == 3){
Serial1.print(F("clutch_m.txt=\"Map 4\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset04);
printEnd();
}
//end
}
int readSize = 0;
int endofOutcomingMessageCount = 0;
int messageend = 0;
String command = "";
void UpdateBaudRate() {
// Update baudrate if required
newBaud = Serial.baud();
if (newBaud != baud) {
baud = newBaud;
HWSERIAL.end();
HWSERIAL.begin(baud);
}
}
void loop() {
if (UploadingMode == 1){
UploadNextionHMI();
}else{
switchUploadMode();
switchMode();
CheckClutch();
CheckAllEncodersHalf();
CheckAllEncodersFull();
CheckAllButtons();
CheckPOV();
if (configMode ==1){
configClutches();
}else{//Added by EG on 07202021 to avoid having Simhub overwriting Elemento
if (HWSERIAL.available())
Serial.write(HWSERIAL.read());
if (Serial.available())
HWSERIAL.write(Serial.read());
while (Serial.available()) {
WriteToComputer();
#ifdef INCLUDE_WS2812B
char c = (char)Serial.read();
if (!LedsDisabled) {
if (messageend < 6) {
if (c == (char)0xFF) {
messageend++;
}
else {
messageend = 0;
}
}
if (messageend >= 3 && c != (char)(0xff)) {
command += c;
while (command.length() < 5) {
WriteToComputer();
while (!Serial.available()) {}
c = (char)Serial.read();
command += c;
}
if (command == "sleds") {
ReadLeds();
}
if (command == "dleds") {
DisableLeds();
}
else {
HWSERIAL.print(command);
}
command = "";
messageend = 0;
}
else {
HWSERIAL.write(c);
}
}
else {
HWSERIAL.write(c);
}
#else
char c = (char)Serial.read();
HWSERIAL.write(c);
#endif
}
WriteToComputer();
}
}
}
void CheckAllButtons(void) {
if (buttbx.getKeys())
{
for (int i=0; i<LIST_MAX; i++)
{
if ( buttbx.key.stateChanged )
{
switch (buttbx.key.kstate) {
case PRESSED:
case HOLD:
Joystick.button(buttbx.key.kchar, 1);
//Added EG 23/03
if (configMode == 1){
//Added EG 07202021
if (buttbx.key.kchar == 4){
buttonClutchPreset = 1;
}
if (buttbx.key.kchar == 6){
button6 = 1;
}
if (buttbx.key.kchar == 10){
button10 = 1;
}
}
if (configMode == 1){
if (buttbx.key.kchar == 7){
button7 = 1;
}
if (buttbx.key.kchar == 8){
button8 = 1;
}
}
//End
break;
case RELEASED:
case IDLE:
Joystick.button(buttbx.key.kchar, 0);
//Added EG 072021
if (buttbx.key.kchar == 4){
buttonClutchPreset = 0;
}
//
//Added EG 23/03
if (buttbx.key.kchar == 6){
button6 = 0;
}
if (buttbx.key.kchar == 10){
button10 = 0;
}
if (buttbx.key.kchar == 7){
button7 = 0;
}
if (buttbx.key.kchar == 8){
button8 = 0;
}
//End
break;
}
}
}
}
}
//HalfStep
void rotary_initHalf() {
for (int i=0;i<NUMROTARIESHALF;i++) {
pinMode(rotariesHalf.pin1, INPUT);
pinMode(rotariesHalf.pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotariesHalf.pin1, HIGH);
digitalWrite(rotariesHalf.pin2, HIGH);
#endif
}
}
unsigned char rotary_processHalf(int _i) {
unsigned char pinstate = (digitalRead(rotariesHalf[_i].pin2) << 1) | digitalRead(rotariesHalf[_i].pin1);
rotariesHalf[_i].state = ttableHalf[rotariesHalf[_i].state & 0xf][pinstate];
return (rotariesHalf[_i].state & 0x30);
}
void CheckAllEncodersHalf(void) {
for (int i=0;i<NUMROTARIESHALF;i++) {
unsigned char result = rotary_processHalf(i);
if (result == DIR_CCW) {
Joystick.button(rotariesHalf.ccwchar, 1); delay(50); Joystick.button(rotariesHalf.ccwchar, 0);
};
if (result == DIR_CW) {
Joystick.button(rotariesHalf.cwchar, 1); delay(50); Joystick.button(rotariesHalf.cwchar, 0);
};
}
}
//End Half Step
//Full Step
void rotary_initFull() {
for (int i=0;i<NUMROTARIESFULL;i++) {
pinMode(rotariesFull.pin1, INPUT);
pinMode(rotariesFull.pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotariesFull.pin1, HIGH);
digitalWrite(rotariesFull.pin2, HIGH);
#endif
}
}
unsigned char rotary_processFull(int _i) {
unsigned char pinstate = (digitalRead(rotariesFull[_i].pin2) << 1) | digitalRead(rotariesFull[_i].pin1);
rotariesFull[_i].state = ttableFull[rotariesFull[_i].state & 0xf][pinstate];
return (rotariesFull[_i].state & 0x30);
}
void CheckAllEncodersFull(void) {
for (int i=0;i<NUMROTARIESFULL;i++) {
unsigned char result = rotary_processFull(i);
if (result == DIR_CCW) {
Joystick.button(rotariesFull.ccwchar, 1); delay(50); Joystick.button(rotariesFull.ccwchar, 0);
};
if (result == DIR_CW) {
Joystick.button(rotariesFull.cwchar, 1); delay(50); Joystick.button(rotariesFull.cwchar, 0);
};
}
}
//End Full Step
void CheckPOV() {
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
if (button0.fallingEdge() && digitalRead(PinButton1) == HIGH && digitalRead(PinButton2) == HIGH && digitalRead(PinButton3) == HIGH && digitalRead(PinButton4) == HIGH ) { // This prevents an accidental PUSH IN to be activated if we are pushing the button Forward, Reverse, Left or Right
Joystick.button(1, 1);
}
if (button1.fallingEdge()) {
Joystick.button(1, 0); // If pushed IN button is active, deactivate it
Joystick.hat(0);
}
if (button2.fallingEdge()) {
Joystick.button(1, 0); // If pushed IN button is active, deactivate it
Joystick.hat(270);
}
if (button3.fallingEdge()) {
Joystick.button(1, 0); // If pushed IN button is active, deactivate it
Joystick.hat(180);
}
if (button4.fallingEdge()) {
Joystick.button(1, 0); // If pushed IN button is active, deactivate it
Joystick.hat(90);
}
if (button0.risingEdge()) {
Joystick.button(1, 0);
}
if (button1.risingEdge()) {
Joystick.hat(-1); }
if (button2.risingEdge()) {
Joystick.hat(-1); }
if (button3.risingEdge()) {
Joystick.hat(-1);
}
if (button4.risingEdge()) {
Joystick.hat(-1);
}
}
//End 07212021
int launchMode = 0;
//
void CheckClutch(){
//Left Clutch
SUMLH = SUMLH - READINGSLH[INDEXLH]; // Remove the oldest entry from the sum
VALUELH = analogRead(CLUTCHLH); // Read the next sensor value
READINGSLH[INDEXLH] = VALUELH; // Add the newest reading to the window
SUMLH = SUMLH + VALUELH; // Add the newest reading to the sum
INDEXLH = (INDEXLH+1) % WINDOW_SIZE; // Increment the index, and wrap to 0 if it exceeds the window size
AVERAGEDLH = SUMLH/ WINDOW_SIZE; // Divide the sum of the window by the window size for the result
//Right Clutch
SUMRH = SUMRH - READINGSRH[INDEXRH]; // Remove the oldest entry from the sum
VALUERH = analogRead(CLUTCHRH); // Read the next sensor value
READINGSRH[INDEXRH] = VALUERH; // Add the newest reading to the window
SUMRH = SUMRH + VALUERH; // Add the newest reading to the sum
INDEXRH = (INDEXRH+1) % WINDOW_SIZE; // Increment the index, and wrap to 0 if it exceeds the window size
AVERAGEDRH = SUMRH/ WINDOW_SIZE; // Divide the sum of the window by the window size for the result
//Added by EG 11/04/21
if (abs(AVERAGEDLHBEFORE - AVERAGEDLH)<2){
AVERAGEDLH = AVERAGEDLHBEFORE;
}
if (abs(AVERAGEDRHBEFORE - AVERAGEDRH)<2){
AVERAGEDRH = AVERAGEDRHBEFORE;
}
//End
//EG 10/04/2021
AVERAGEDLHMAPPED = map(MAXLH-AVERAGEDLH,0,CLUTCHRANGELH,0,100);
AVERAGEDRHMAPPED = map(MAXRH-AVERAGEDRH,0,CLUTCHRANGERH,0,100);
//AVERAGEDCLUTCH = (AVERAGEDRHMAPPED*BITINGPOINT/100)+(AVERAGEDLHMAPPED*(1-BITINGPOINT/100));
//End
//EG 07212021
AVERAGEDCLUTCH = (AVERAGEDRHMAPPED*BITINGPOINT/100)+(AVERAGEDLHMAPPED*(1-BITINGPOINT/100));
if (AVERAGEDCLUTCH<0){
AVERAGEDCLUTCH = 0;
}
if (AVERAGEDRHMAPPED>5){
launchMode = 1;
}
if (AVERAGEDCLUTCH==0){
launchMode = 0;
}
if (launchMode == 0){
AVERAGEDCLUTCH = AVERAGEDLHMAPPED;
}else{
AVERAGEDCLUTCH = (AVERAGEDRHMAPPED*BITINGPOINT/100)+(AVERAGEDLHMAPPED*(1-BITINGPOINT/100));
}
if (AVERAGEDCLUTCH<0){
AVERAGEDCLUTCH = 0;
}
//End
Joystick.Z(map(AVERAGEDCLUTCH,0,100,0,1023));
delay(0);
AVERAGEDLHBEFORE = AVERAGEDLH;
AVERAGEDRHBEFORE = AVERAGEDRH;
}
//Added by EG 22/03/21
//Config Functions
void configModeChange(){
if (UploadingMode == 0){
if (configMode==0){
configMode = 1;
isMaxCalibrationBeenDone = 0;
isMinCalibrationBeenDone = 0;
setLedConfig(0x00FFFF);
setLedConfigUnSaved(0xFFFF00);
//Serial.println("Config mode entered");
HWSERIAL.end();
HWSERIAL.begin(921600);
delay(500);
HWSERIAL.print("page 1");
printEnd();
}else if (configMode == 1){
HWSERIAL.print("page 0");
printEnd();
HWSERIAL.end();
HWSERIAL.begin(921600);
//Serial.println("Config mode exited");
delay(500);
configMode = 0;
setLedConfig(0x000000);
setLedConfigUnSaved(0x000000);
}
}
}
int COMBINEDCLUTCHCONFIGTOSEND;
int CLUTCHLHCONFIG;
int CLUTCHRHCONFIG;
float COMBINEDCLUTCHCONFIG;
void configClutches(){
clutchMaxCalibration();
clutchMinCalibration();
CheckClutch();
//EG 07202021
changeClutchBitePreset();
//End
UpdateBitingPoint();
//Serial.println(BITINGPOINT);
CLUTCHLHCONFIG = map(MAXLH-AVERAGEDLH,0,CLUTCHRANGELH,0,100);
CLUTCHRHCONFIG= map(MAXRH-AVERAGEDRH,0,CLUTCHRANGERH,0,100);
HWSERIAL.print("j0.val=");
HWSERIAL.print(CLUTCHLHCONFIG);
printEnd();
HWSERIAL.print("j1.val=");
HWSERIAL.print(CLUTCHRHCONFIG);
printEnd();
CLUTCHRHCONFIG = CLUTCHRHCONFIG*BITINGPOINT/100;
CLUTCHLHCONFIG = CLUTCHLHCONFIG*(1-BITINGPOINT/100);
COMBINEDCLUTCHCONFIG = CLUTCHLHCONFIG+ CLUTCHRHCONFIG;
//Serial.println(CLUTCHRHCONFIG);
//Serial.println(CLUTCHLHCONFIG);
//Serial.println(COMBINEDCLUTCHCONFIG);
COMBINEDCLUTCHCONFIGTOSEND= COMBINEDCLUTCHCONFIG;
HWSERIAL.print("j2.val=");
HWSERIAL.print(COMBINEDCLUTCHCONFIGTOSEND);
printEnd();
HWSERIAL.print("n0.val=");
HWSERIAL.print(BITINGPOINT/100);
printEnd();
saveClutchConfig();
}
void printEnd(){
HWSERIAL.write(0xff);
HWSERIAL.write(0xff);
HWSERIAL.write(0xff);
}
void switchMode(){
currentButtonState = digitalRead(PinButton0);
if(lastButtonState == HIGH && currentButtonState == LOW){ // button is pressed
pressedTime = millis();
pressStarted = 1;
}
if (pressStarted ==1){
if (currentButtonState == LOW){
//Serial.println("pressed");
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
//Serial.println(pressDuration/1000);
if( pressDuration > 3000 ){
//Serial.println("Changing Mode");
//configModeChange();
UppressStarted = 0;
pressStarted = 0;
isConfigAlreadyBeenSaved = 0;
configModeChange();
//Serial.println(configMode);
//Serial.println("Mode Changed");
}
}
}
lastButtonState = currentButtonState;
}
int currentButton6State = 0;
int lastButton6State = 0;
int currentButton10State = 0;
int lastButton10State = 0;
void saveClutchConfig(){
if (button6 == 1 & button10 == 1 & isConfigAlreadyBeenSaved == 0){
//Serial.println("Config has been saved");
EEPROM.write(MAXLHaddress,MAXLH/4);
EEPROM.write(MAXRHaddress,MAXRH/4);
EEPROM.write(MINLHaddress,MINLH/4);
EEPROM.write(MINRHaddress,MINRH/4);
EEPROM.write(BITINGPOINTaddress,BITINGPOINT);
//EG 07212021
EEPROM.write(BITINGPOINT01address,preset01);
EEPROM.write(BITINGPOINT02address,preset02);
EEPROM.write(BITINGPOINT03address,preset03);
EEPROM.write(BITINGPOINT04address,preset04);
//
setLedConfigSaved(0x00FF00);
isConfigAlreadyBeenSaved = 1;
}
}
void UpdateBitingPoint(){
currentButton6State = button6;
currentButton10State = button10;
//EG 07212021
if (selectedPreset == 0){
BITINGPOINT = preset01;
}
if (selectedPreset == 1){
BITINGPOINT = preset02;
}
if (selectedPreset == 2){
BITINGPOINT = preset03;
}
if (selectedPreset == 3){
BITINGPOINT = preset04;
}
//End
if(currentButton6State == 1 & lastButton6State == 0){ // button is pressed
BITINGPOINT = BITINGPOINT +5;
}
if(currentButton10State == 1 & lastButton10State == 0){ // button is pressed
BITINGPOINT = BITINGPOINT -5;
}
if (BITINGPOINT>100){
BITINGPOINT = 100;
}else if (BITINGPOINT <0){
BITINGPOINT = 0;
}
lastButton6State = currentButton6State;
lastButton10State = currentButton10State;
//EG 07212021
if (selectedPreset == 0){
preset01 = BITINGPOINT;
}
if (selectedPreset == 1){
preset02 = BITINGPOINT;
}
if (selectedPreset == 2){
preset03 = BITINGPOINT;
}
if (selectedPreset == 3){
preset04 = BITINGPOINT;
}
//End
}
void setLedConfig(uint32_t color){
WS2812B_strip.setBrightness(50);
WS2812B_strip.setPixelColor(0,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(1,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(2,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(15,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(16,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(17,color);
WS2812B_strip.show();
}
void setLedConfigSaved(uint32_t color){
WS2812B_strip.setBrightness(50);
WS2812B_strip.setPixelColor(3,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(14,color);
WS2812B_strip.show();
}
void setLedConfigUnSaved(uint32_t color){
WS2812B_strip.setBrightness(50);
WS2812B_strip.setPixelColor(3,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(14,color);
WS2812B_strip.show();
}
void clutchMaxCalibration(){
if (button7 == 1 & button8 == 1 & isMaxCalibrationBeenDone == 0){
MAXLH = 1023;
MAXRH = 1023;
clutchMaxCalibrationLed(0x00FFFF);
//Serial.println("CurrentMax");
//Serial.println(MAXLH);
isMaxCalibrationBeenDone = 1;
for (int i = 1; i<10000;i++){
//Serial.println(i);
CheckClutch();
if (AVERAGEDLH<MAXLH){
MAXLH =AVERAGEDLH;
}
if (AVERAGEDRH<MAXRH){
MAXRH =AVERAGEDRH;
}
}
MAXLH = MAXLH-2;
MAXRH = MAXRH-2;
//Serial.println("NexMax");
//Serial.println(MAXLH);
clutchMaxCalibrationLed(0x000000);
}
}
void clutchMinCalibration(){
if (button7 == 1 & button8 == 1 & isMinCalibrationBeenDone == 0){
MINLH = 1023;
MINRH = 1023;
clutchMinCalibrationLed(0x00FFFF);
//Serial.println("CurrentMin");
//Serial.println(MINLH);
isMinCalibrationBeenDone = 1;
for (int i = 1; i<10000;i++){
CheckClutch();
//Serial.println(AVERAGEDLH);
if (AVERAGEDLH<MINLH){
MINLH =AVERAGEDLH;
}
if (AVERAGEDRH<MINRH){
MINRH =AVERAGEDRH;
}
}
MINLH = MINLH +2;
MINRH = MINRH +2;
CLUTCHRANGELH = (MAXLH-MINLH);
CLUTCHRANGERH = (MAXRH-MINRH);
CLUTCHRANGE = CLUTCHRANGELH +CLUTCHRANGERH;
clutchMinCalibrationLed(0x000000);
}
}
void clutchMaxCalibrationLed(uint32_t color){
WS2812B_strip.setBrightness(30);
WS2812B_strip.setPixelColor(4,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(5,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(6,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(7,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(8,color);
WS2812B_strip.show();
}
void clutchMinCalibrationLed(uint32_t color){
WS2812B_strip.setBrightness(30);
WS2812B_strip.setPixelColor(9,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(10,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(11,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(12,color);
WS2812B_strip.show();
WS2812B_strip.setPixelColor(13,color);
WS2812B_strip.show();
}
//Upload Mode function
void UploadeModeChange(){
if (configMode == 0){
if (UploadingMode==0){
UploadingMode= 1;
setLedConfig(0xFF0000);
}else if (UploadingMode== 1){
UploadingMode= 0;
setLedConfig(0x000000);
}
}
}
void switchUploadMode(){
UpcurrentButtonState = digitalRead(PinButton1);
if(UplastButtonState == HIGH && UpcurrentButtonState == LOW){ // button is pressed
UppressedTime = millis();
UppressStarted = 1;
}
if (UppressStarted ==1){
if (UpcurrentButtonState == LOW){
//Serial.println("pressed");
UpreleasedTime = millis();
long UppressDuration = UpreleasedTime - UppressedTime;
//Serial.println(pressDuration/1000);
if( UppressDuration > 2000 ){
pressStarted = 0;
//Serial.println("Changing Mode to Uploading Mode");
//configModeChange();
UppressStarted = 0;
UploadeModeChange();
//Serial.println(configMode);
//Serial.println("Mode Changed");
}
}
}
UplastButtonState = UpcurrentButtonState;
}
//EG 07202021 - Clutche Biting point preset
int currentbuttonClutchPresetState = 0;
int lastbuttonClutchPresetState = 0;
void changeClutchBitePreset (){
currentbuttonClutchPresetState = buttonClutchPreset;
if(currentbuttonClutchPresetState == 1 & lastbuttonClutchPresetState == 0){ // button is pressed
selectedPreset = selectedPreset +1;
if (selectedPreset>3){
selectedPreset =0;
}
}
if (selectedPreset == 0){
Serial1.print(F("clutch_m.txt=\"Map 1\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset01);
printEnd();
}
if (selectedPreset == 1){
Serial1.print(F("clutch_m.txt=\"Map 2\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset02);
printEnd();
}
if (selectedPreset == 2){
Serial1.print(F("clutch_m.txt=\"Map 3\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset03);
printEnd();
}
if (selectedPreset == 3){
Serial1.print(F("clutch_m.txt=\"Map 4\""));
printEnd();
Serial1.print(F("clutch_v.val="));
Serial1.print(preset04);
printEnd();
}
lastbuttonClutchPresetState = currentbuttonClutchPresetState;
}
When I try to calibrate the analog signal (A0 and A1) with the Teensy 3.6 it works correctly but with the 4.1 it doesn't.
Compared to the video in the link (video) which is working with teensy 3.6 the LED switching times are much quicker
Furthermore, teensy 4.1 does not send information to the connected Nextion monitor.
I have only this problem the other function: button, pov, rotary and enter and out to configuration mode are ok
Does anyone have any advice for me on this.