Cruise Control emulation.

Hello, I decided to try to emulate a cruise control system as my 1st project after going through the Arduino projects book, as I thought it would be fairly achievable.

Unfortunately I’ve fallen at the first hurdle: trying to emulate an accelerator pedal! :sweat_smile:

Currently I have it set up so I push a button (accelerator pedal), which causes an LED to illuminate and the variable speedometer is incremented, this part works ok, but I want it to count down when the button is released but it shoots straight to “-1”.

“-1” is the second part i don’t understand as I’ve set a “for” speedometer >=0, however I don’t think this part of the code is being registered as i can change it to >=10 etc and it still ends up as “-1”

I’ve tried it with do and while but that way it doesn’t stop at all and just keeps counting down -ve forever.

Any help gratefully received, the code is probably really inefficient too but i’m not too worried about that at the moment, i haven’t done any coding since a computer science a-level when i was 13, over a decade and a half ago now (probably explains a few things), trying to pick it up again with Arduino and Pi. I’m a mechanical / materials / forensic engineer in my day to day job.


const int gasPedal = 2; //Accelerator input pin
const int brakeClutch = 3; //brake or clutch input pin
const int cruiseSau = 4; //Cruise mode activate input
const int cruiseSet = 5; //Cruise set point pin

const int LedAccel = 8; //LED output gas
const int LedCruiseArmed = 9; //LED cruise mode active
const int LedCruiseActive = 10; //LED cruise control on.

int speedometer = 0; //speedo
int accelState = 0; // is the accelerator on or off

void setup(){
  // Set pins 2-5 as inputs:
  for (int x = 2;x<6;x++){
    pinMode(x, INPUT);
  // Set pins 8-10 as outputs:
  for (int y = 8; y < 11; y++){
    pinMode(y, OUTPUT);

void loop(){
  digitalWrite(LedAccel, LOW);
  accelState = digitalRead(gasPedal);
  Serial.print("Accelerator: ");
  Serial.print(", Speed: ");
  //Speed Simulation:
  if (accelState == HIGH){
    digitalWrite(LedAccel, HIGH);
    speedometer++ ;
  else {
    for(speedometer >= 0; speedometer --;); //decrease speed until 0
      Serial.print("Accelerator: ");
      Serial.print(accelState );
      Serial.print(", Speed: ");

Looks to me like your "for" has two associated problems:

  • The ; on the end
  • No {} to circle the lines you need to activate

Thank you Jimbo, i get a different behavior after implementing your observation, but it’s still not quite right.

  • The speedometer counts down continuously now unless I hold the accelerator whilst uploading the program
  • Whilst the speedometer is above “-1” it can be increased again with the gasPedal
  • if speedometer is allowed to reach “-2” gasPedal button no longer works and the first behavior continues

I’m going to try and understand it myself now you’ve bumped me on a stage, but sure i’ll be back soon!! :roll_eyes:

Thanks again,

Unfortunately I've fallen at the first hurdle: trying to emulate an accelerator pedal!

I would think a pot would be better to simulate the accelerator pedal than a push button.