edited for clarity
When I push menu up or down, HTog gets inverted, which starts the horn beeping command. From what I can tell, nothing in the horn or horn button's routine even mentions up or down.
Detailed explanation on line 271.
code with comments that explain things:
#include <SoftwareSerial.h>
#include "SSD1306Ascii.h"
#define I2C_ADDRESS 0x3C//serial link to bike's controller
#define AGREE_TO_TERMS false//used in person 2's code to prevent/allow speeds above 19 mph
#define up 4//menu up
#define Horn_b 5//horn button
#define down 6//menu down
#define RTurn 7//right turn signal button
#define LTurn 8//left turn signal button
#define Brakes 12//brake light's button
#define RLED 9//right turn signal LED
#define LLED 10//left turn signal LED
#define Brakes_LED 11
#define Horn A0//actual horn/piezoelectric buzzer
#define RST_PIN -1//no idea. from person 3's code for setting up the serial link between arduino and display
SSD1306AsciiAvrI2c oled;
SoftwareSerial mySerial(2, 3); // RX, TX
unsigned long RpreviousMillis = 0;//variable used for right turn signal delay
unsigned long LpreviousMillis = 0;//variable used for left turn signal delay
unsigned long HpreviousMillis = 0;//variable used for horn beeping delay
unsigned long HbpreviousMillis = 0;//variable used to keep track of horn button press length
unsigned long oledpreviousMillis = 0;//variable used for screen refresh rate (gets kinda weird if too quick)
const long interval = 500;//turn signal interval
int Tmax = 150;//max motor temperature (Celcius) considered "safe"
int speed_ = 0.0;//top actual speed
bool RState = 0;//is right turn signal on or off?
bool LState = 0;//is left turn signal on or off?
bool RTog = 0;//triggers right turn signal
bool LTog = 0;//triggers left turn signal
bool HTog = 0;//triggers horn beeping
bool sel = 0;//changes up/down function
bool up_var = 0;//is button pushed?
bool Horn_var = 0;//is button pushed?
bool down_var = 0;//is button pushed?
bool RTurn_var = 0;//is button pushed?
bool LTurn_var = 0;//is button pushed?
bool Brakes_var = 0;//is button pushed?
int TMPH = 15;//current max speed
int kmph = TMPH * 1.609; //Convert mph to kmph
byte bA[] = {0xaa, 0x06, 0x06, kmph, 0x00, 0xbb}; //Initialize byte array
byte headlightsOn[] = {0xaa, 0x07, 0x06, 0x01, 0xaa, 0xbb};
byte headlightsOff[] = {0xaa, 0x07, 0x06, 0x00, 0xab, 0xbb};
//variables for text to display
String yel = String(11);
String blu1 = String(11);
String blu2 = String(11);
String blu3 = String(11);
//text end
//person 2's arduino to bike wizardry
int maxSpeedKM = 0; // kmph
float speedRPM = 0.0; // RPM
float speed = (int(speedRPM * 43.98229715 * 10) / 10); //MPH
int temperature = 0; // Celsius
int temperature_ = 0;
float voltage = 0.0f; // UNKNOWN
int odo = 0; // unknown format
int runTime = 0; // Minutes
int headlight = 0; // 0x00 off | 0x10 on
int incoming;
int incomingArr[9];
int index = 0;
void printRegister( int arr[] ) {
for (int i = 0; i < 9; i++) {
if (arr[i] <= 0x0f)
Serial.print(0);
Serial.print(arr[i], HEX);
}
Serial.println(0xbb, HEX);
}
int checkSum(int inArray[], int arraySize) {
int checksum = 0x00;
for(int i = 0; i < arraySize - 2; i++)
checksum ^= inArray[i];
return checksum;
}
//person 2's code ends
void setup()
{
//no clue why this is necessary
#define RST_PIN -1
#if RST_PIN >= 0
oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else
oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif
//still no clue
//text
yel = String("-----------");
blu1 = String(" MAX:" + String(TMPH) + "----");
blu2 = String(" " + String(speed,1) + "-" + String(speed_,1));
blu3 = String(" " + String(temperature) + "C-" + String(temperature_) + "C");
//text
oled.setFont(System5x7);
oled.clear();
oled.set2X();
oled.println(yel);
oled.println(blu1);
oled.println(blu2);
oled.println(blu3);
pinMode(LLED,OUTPUT);
pinMode(RLED,OUTPUT);
pinMode(up,INPUT_PULLUP);
pinMode(Horn_b,INPUT_PULLUP);
pinMode(down,INPUT_PULLUP);
pinMode(LTurn,INPUT_PULLUP);
pinMode(RTurn,INPUT_PULLUP);
pinMode(Brakes,INPUT_PULLUP);
pinMode(Brakes_LED,OUTPUT);
pinMode(Horn,OUTPUT);
//person 2's stuff
// Open serial communications 115200 BAUD
Serial.begin(115200);
while (!Serial) { ; }
// set the data rate for the SoftwareSerial port
mySerial.begin(115200);
delay(200); // Wait for Jetson controller to boot
//read bike's controller start
if (mySerial.available())
{
incoming = mySerial.read();
if (incoming != 0xBB)
{
incomingArr[index] = incoming;
index++;
}
else
{
if (checkSum(incomingArr, 10) == incomingArr[8])
{
if (incomingArr[1] == 0xA1)
{
speedRPM = (incomingArr[5-1] * 0xff) + incomingArr[6-1];
temperature = incomingArr[8-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA2)
{
odo = incomingArr[5-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA3)
{
maxSpeedKM = incomingArr[4-1];
runTime = incomingArr[8-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA4)
{
headlight = incomingArr[8-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA7)
{
printRegister(incomingArr);
}
}
index = 0;
}
}
//read bike's controller end
if (TMPH < 3) // Only accept values higher than 3
{
kmph = 3;
}
if (kmph > 0xff && AGREE_TO_TERMS) // Don't allow setting higher than 0xff (255)
{
kmph = 0xff;
}
if (kmph > 30 && !AGREE_TO_TERMS) // If the user has not agreed to terms
{
kmph = 30;
} // Do not accept speed higher than 19 mph (30 kmph)
bA[3] = kmph;
int SizeOfArray = sizeof(bA) / sizeof(bA[0]);
bA[4] = checkSum(bA, SizeOfArray); //Calculate and add the checksum to byte array
for(int i = 0; i < SizeOfArray; i++)
{
//Write bytes serially to pin 3. This should be connected to the GREY wire of the Jetson Bolt Pro
mySerial.write(bA[i]);
}
}
byte checkSum(byte byteArray[], int arraySize)
{
byte checksum = 0x00;
for(int i = 0; i < arraySize - 2; i++)
checksum ^= byteArray[i];
return checksum;
}
//person 2's code end
void loop()
{
up_var = !digitalRead(up);
Horn_var = !digitalRead(Horn_b);
down_var = !digitalRead(down);
RTurn_var = !digitalRead(RTurn);//inverted reads in this section are to make on=1 and off=0
LTurn_var = !digitalRead(LTurn);
Brakes_var = digitalRead(Brakes);//brakes are already inverted
unsigned long currentMillis = millis();
//read bike's controller start (pulled from person 2's code)
if (mySerial.available())
{
incoming = mySerial.read();
if (incoming != 0xBB)
{
incomingArr[index] = incoming;
index++;
}
else
{
if (checkSum(incomingArr, 10) == incomingArr[8])
{
if (incomingArr[1] == 0xA1)
{
speedRPM = (incomingArr[5-1] * 0xff) + incomingArr[6-1];
temperature = incomingArr[8-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA2)
{
odo = incomingArr[5-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA3)
{
maxSpeedKM = incomingArr[4-1];
runTime = incomingArr[8-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA4)
{
headlight = incomingArr[8-1];
printRegister(incomingArr);
}
else if (incomingArr[1] == 0xA7)
{
printRegister(incomingArr);
}
}
index = 0;
}
}
//read bike's controller end (pulled from person 2's code)
/*problem area start
This is supposed to be set up to have a variable's value match the current
time until the button is pressed. This section should then do nothing until
the button is released and determine if the button was pressed for a short
duration (tap) or a long duration (hold). If it was a tap, routine 1 should be
the only one activated. If it was a hold, routine 2 should be the only one
activated. This works as intended with the horn button (Horn_b & Horn_var),
but the tap routine activates even when up/down are pressed for any length of time.
*/
//Horn button
if (Horn_var == 0 && currentMillis - HbpreviousMillis < 1500 && currentMillis - HbpreviousMillis > 120)
{
HTog = !HTog;//short push (tap) tells horn to start beeping
}
if (Horn_var == 0 && currentMillis - HbpreviousMillis >= 1500)
{
sel = !sel;//long push (hold) changes function of up/down
}
if (Horn_var == 0)
{
HbpreviousMillis = currentMillis;//tries to match current time until button is pushed
}
//Horn button end
//beep start
if (HTog == 1 && currentMillis - HpreviousMillis >= 300)
{
digitalWrite(Horn,HIGH);
delay(10);
digitalWrite(Horn,LOW);
HpreviousMillis = currentMillis;
}
//beep end
//problem area end (hopefully)
if (RTurn_var == 1 && LTurn_var == 0)
{
RTog = !RTog;//toggles right turn signal
LTog = 0;//turns left turn signal off
delay(250);//debounce
}
if (LTurn_var == 1 && RTurn_var == 0)
{
LTog = !LTog;//toggles left turn signal
RTog = 0;//turns right turn signal off
delay(250);//debounce
}
if (Brakes_var == 1)
{
analogWrite(Brakes_LED,255);//brake light is brighter
}
else
{
analogWrite(Brakes_LED,70);//running light is dimmer brake
}
if (RTog == 1 && currentMillis - RpreviousMillis >= interval && RState == 0)
{//if right turn signal is off & routine is on, and wait time is correct, turns light on and makes sure left is off
digitalWrite(RLED,HIGH);
RState = 1;
LState = 0;
yel = "---------->";//updates screen line 1
RpreviousMillis = currentMillis;
}
else if (RTog == 1 && currentMillis - RpreviousMillis >= interval && RState == 1)
{//if right turn signal is on & routine is on, and wait time is correct, turns light off and makes sure left is off
digitalWrite(RLED,LOW);
RState = 0;
LState = 0;
RpreviousMillis = currentMillis;
}
if (LTog == 1 && currentMillis - LpreviousMillis >= interval && LState == 0)
{//if left turn signal is off & routine is on, and wait time is correct, turns light on and makes sure right is off
digitalWrite(LLED,HIGH);
LState = 1;
RState = 0;
yel = "<----------";//updates screen line 1
LpreviousMillis = currentMillis;
}
else if (LTog == 1 && currentMillis - LpreviousMillis >= interval && LState == 1)
{//if left turn signal is on & routine is on, and wait time is correct, turns light off and makes sure right is off
digitalWrite(LLED,LOW);
LState = 0;
RState = 0;
LpreviousMillis = currentMillis;
}
if (LTog == 0 && RTog == 0)
{//if both turn signal routines are off, turns both LEDs off
digitalWrite(LLED,LOW);
digitalWrite(RLED,LOW);
LState = 0;
RState = 0;
yel = "-----------";//updates screen line 1
}
//menu
if (sel == 0)
{//menu mode 1 controlled by horn button hold
blu1 = String(" MAX:" + String(TMPH) + "----");
blu2 = String(">" + String(speed,1) + "-" + String(speed_,1));
blu3 = String(">" + String(temperature) + "C-" + String(temperature_) + "C");
if (up_var == 1 && up_var + down_var == 1)
{
speed_ = speed;//up resets highest speed (spedometer) to current speed (spedometer)
}
if (down_var == 1 && up_var + down_var == 1)
{
temperature_ = temperature;//down resets highest temperature reached to current temperature
}
}
if (sel == 1)
{//menu mode 2 controlled by horn button hold
blu1 = String(">MAX:" + String(TMPH) + "----");
blu2 = String(" " + String(speed,1) + "-" + String(speed_,1));
blu3 = String(" " + String(temperature) + "C-" + String(temperature_) + "C");
if (up_var == 1 && up_var + down_var == 1 && TMPH < 19)
{//up raises max speed on motor
TMPH = TMPH + 1;
kmph = (TMPH) * 1.609;
delay(250);
}
if (down_var == 1 && up_var + down_var == 1 && TMPH > 11)
{//down lowers max speed on motor
TMPH = TMPH - 1;
kmph = (TMPH) * 1.609;
delay(250);
}
}
//menu
if (temperature >= Tmax && TMPH > 15)
{//if temperature is too high, lowers max speed to 15 mph
TMPH = 15;
kmph = (TMPH) * 1.609;
up_var = 0;
down_var = 1;
}
//more arduino to bike communication wizardry
if (up_var + down_var == 1)
{
bA[3] = kmph;
int SizeOfArray = sizeof(bA) / sizeof(bA[0]);
bA[4] = checkSum(bA, SizeOfArray); //Calculate and add the checksum to byte array
for(int i = 0; i < SizeOfArray; i++)
{
//Write bytes serially to pin 3. This should be connected to the GREY wire of the Jetson Bolt Pro
mySerial.write(bA[i]);
}
}
//wizardry ends
if (currentMillis - oledpreviousMillis >= 500)
{//if refresh rate reached, refreshes screen
oled.clear();
oled.println(yel);
oled.println(blu1);
oled.println(blu2);
oled.println(blu3);
oledpreviousMillis = currentMillis;
}
}
better picture: