Close

Retarder Handle Part 4

A project log for Real Scania Truck Home Simulator

The Scrapyard and Arduino make a great combination!

jeroen-van-der-veldenJeroen van der Velden 12/13/2015 at 12:010 Comments

So close thanks to member OUTSIDER on the Arduino Forums!

The code below will tell us the handle position and difference between all positions just like in a real Scania!

Final challenge; keyboardPrint`s!

unsigned long startTime, endTime = 1000;
byte handlePos, oldHandlePos, currentPos;
int8_t diff;
int val;
bool posSent = true;

void setup()
{
  Serial.begin(9600);
  val = getAnalog();
  Serial.print("\nCurrent handle position = ");
  Serial.print(handlePos);
  Serial.println("\n\n");
  currentPos = oldHandlePos = handlePos;
}

void loop()
{
  val = getAnalog();
  if(handlePos != currentPos)
  {
    startTime = millis();
    currentPos = handlePos;
    posSent = false;
    diff = 0;
  }  
    if(!posSent && millis() - startTime > endTime)
    {
      Serial.print("Handle Position =  ");
      Serial.print(handlePos);
      Serial.print("\t");
      diff = handlePos - oldHandlePos;
      Serial.print("Difference =  ");
      Serial.println(diff);
      oldHandlePos = handlePos;
      posSent = true;
    }
}

int getAnalog()
{
    val = analogRead(0);
  if(val < 187) handlePos = 0;
    else if(val < 350) handlePos = 1;
    else if(val < 518) handlePos = 2;
    else if(val < 679) handlePos = 3;
    else if(val < 861) handlePos = 4;
    else handlePos = 5;
    return val;
}
// by Jeroen van der Velden
// https://hackaday.io/project/8448-real-dashboard-truck-simulator
// Credits to Arduino Forum member "OUTSIDER"

Discussions