Motorcycle Cruise Control Cancel Command

A brief introduction as this is my first post.

Please let me know if I have done something in this post that is incorrect.

I have always been interested in learning to program in c++ but I have never taken the leap, until now. I am known for jumping in head first and I believe that this simple project has turned into that scenario.

I started this endeavor with the thought of saving a few dollars by making my own head light modulator / brake light flasher instead of paying >$100.

Once I learned the very basics and watched a few videos viola, a functioning device for around $15!

I then decided to take it a step further and try my hand at adding a cruise control function.

Before you get to reading my current coding, understand that while I do understand 95% of whats in it, most of it is copying others handy work or examples and modifying it to suit my needs.

That brings us to the here and now…

I have everything working as it should except for a “cruiseCancel” IE when the brake is applied, set cruiseEnable to LOW -immediately!-

I have tried if commands before and after the setSWdown portion as well as at the beginning of the loop and the end.

I have tried an else statement after the setSWdown portion as well.

Everything I try seems to set my cruiseEnable to HIGH no matter the state of the power to the circuit or the state of the brake.

As for the circuit, I have included a graphic, before you ask, My bike uses a combination tail light and brake light wire. while the brake is applied it is at nominal voltage, while the brake is not applied, it rests at 9.4 volts.

I currently have a breadboard and arduino UNO set up and have verified that the code is working except for a cancel command.

I have the code in to be able to operate a stepper driver but I don’t have one yet and want to do this one step at a time.

Please don’t tell me how horrible my code looks, I am going to clean it up more as I learn!

Yes I know that this is dangerous.
Yes I am going to implement mechanical bypasses.
Yes I know this a in depth sketch for my first one.
Yes I understand that these boards and circuits can fail.
No I don’t need to be reminded that its dangerous.

/*
  
*/


const int brakeLight =  7;
const int  brakeSwitch = A0;
const int highBeam =  6;
const int  highBeamSwitch = 8;

int brakeSwitchState = analogRead(brakeSwitch);
int brakeState = HIGH;
int counter = 0;         
int blinkCount = 7;
int highBeamState = LOW;             
int highBeamSwitchState = 0;
int dayLight = 0;  
int dayLightPin = A1;    
int dusk = 200;
int voltageThreshold = 300;

int hall = 3;//IRQ1 
int cruiseEnable = 13;
int cruiseStep = 10;
int cruiseDirection =11; 
int setSwitch = 12;  
int cruisePower = digitalRead(9);

int gap;
int setSWdown;
volatile unsigned int speedTick=0;
volatile unsigned int prevSpeedTick;
volatile unsigned int speedSet;
volatile unsigned int tick;

void hallISR()  {
                  prevSpeedTick = speedTick; // buffer so speedTick can be used as prevSpeedTick
                  during compares      
                  speedTick = 0;
                }

unsigned long previousMillis = 0;        
unsigned long previousMillis1 = 0;


const long interval = 100;
const long interval1 = 120;

//Void Setup-------------------------------------------------------------------------------------------

void setup() {

  pinMode(brakeLight, OUTPUT);
  pinMode(highBeam, OUTPUT);
  pinMode(brakeSwitch, INPUT);

  pinMode(hall, INPUT); 
  pinMode(cruiseEnable,OUTPUT);
  pinMode(cruiseStep,OUTPUT);
  pinMode(cruiseDirection,OUTPUT);
  pinMode(setSwitch, INPUT); 
  Serial.begin(9600);

      cli(); // stop interrupts
    TCCR2A = 0; // set entire TCCR2A register to 0
    TCCR2B = 0; // same for TCCR2B
    TCNT2  = 0; // initialize counter value to 0
    // set compare match register for 2000 Hz increments
    OCR2A = 249; // = 16000000 / (32 * 2000) - 1 (must be <256)
    // turn on CTC mode
    TCCR2B |= (1 << WGM21);
    // Set CS22, CS21 and CS20 bits for 32 prescaler
    TCCR2B |= (0 << CS22) | (1 << CS21) | (1 << CS20);
    // enable timer compare interrupt
    TIMSK2 |= (1 << OCIE2A);
    sei(); // allow interrupts 

    // HARDWARE INTERRUPT on pin 3(hall effect) attached to hall effect device
    attachInterrupt(digitalPinToInterrupt(hall), hallISR, FALLING);
}

  ISR(TIMER2_COMPA_vect)
{
   speedTick=speedTick+1; 
 }
 
//Void Loop--------------------------------------------------------------------------------------------

void loop() {

  Serial.println(brakeSwitchState);
  delay(1);
  
  dayLight = analogRead(dayLightPin);
  
  brakeSwitchState = analogRead(brakeSwitch); 

  setSWdown = digitalRead(setSwitch);
  
//Cruise Control---------------------------------------------------------------------------------------
// THIS IS THE PART where I'm having difficulty ---------------------------------------------------


  if (setSWdown == LOW)
          {
              speedSet = prevSpeedTick;
              gap = speedSet / 40;
              digitalWrite(cruiseEnable,HIGH); 
           } 

  if (prevSpeedTick > speedSet + gap) //too slow
    {
      openThrottle();
    }

  if (prevSpeedTick < speedSet - gap ) //too fast
    {
      closeThrottle();
     }

//-------------------------------------------------------------------------------------------------------
     
//Brake Flasher----------------------------------------------------------------------------------------

   brakeSwitchState = analogRead(brakeSwitch);

   if ((brakeSwitchState > voltageThreshold) && (counter <= blinkCount))  {
  
  unsigned long currentMillis = millis();
  

  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;


    if (brakeState == LOW) {
          brakeState = HIGH;
        }   else {
             brakeState = LOW;
             } 
    
     counter++;
    
    
    
    


  digitalWrite(brakeLight, brakeState);
  }
 
  
  

  } else {
    
          if (brakeSwitchState > voltageThreshold){
          digitalWrite(brakeLight, HIGH);
          } else {
            digitalWrite(brakeLight, LOW);
          }
          if(brakeSwitchState < voltageThreshold)  {
          counter = 0;
          }

          }
          
//Headlight Modulator----------------------------------------------------------------------------------

    highBeamSwitchState = digitalRead(highBeamSwitch);
    
           if ((dayLight >= dusk) && (highBeamSwitchState == HIGH)) {
                
              
           unsigned long currentMillis1 = millis();
          
           if (currentMillis1 - previousMillis1 >= interval1) {
          
              previousMillis1 = currentMillis1;
          
           if (highBeamState == LOW) {
                highBeamState = HIGH;
              }     else {
                     highBeamState = LOW;
                     }
          
            digitalWrite(highBeam, highBeamState);

           }
            }     else {
            
                      if (highBeamSwitchState == HIGH)  {
                         digitalWrite(highBeam, HIGH);
                         }     else {
                                       digitalWrite(highBeam, LOW);
                                       }
                          }
  
 
}

//Stepper Control--------------------------------------------------------------------------------------

void openThrottle ()
        {
        digitalWrite(cruiseDirection,HIGH);
        digitalWrite(cruiseStep,HIGH);
        delay(1);
        digitalWrite(cruiseStep,LOW);
        delay(1);
        }
    
void closeThrottle ()
        {
        digitalWrite(cruiseDirection,LOW);
        digitalWrite(cruiseStep,HIGH);
        delay(1);
        digitalWrite(cruiseStep,LOW);
        delay(1);
    }