Spent the morning cleaning up the code, changing all the “button” references to “sensor” for that eventual day when I get an actual sensor, and started fiddling about with the other three values that are going to show up on the display, namely system voltage, water temperature and oil pressure. It was in doing these that I realized I was outputting the MPH in reverse. I hadn’t noticed because it was so hard to get a two digit MPH reading mashing on the button. So I fixed that, and streamlined the code a bit. I find that programming for 32Kb of storage really helps you focus on tightening up sloppy code. And man, do I write sloppy code. I am so out of practice. So I created sections for the other three sensors, and just had them generate random numbers in approximately the right range so I could format the output to the display, all while NOT breaking the code for calculating MPH. So far, so good:
Here’s the code as it stands right now:
/* Gauges (Gauges? We don't need no stinkin' gauges) Digital gauges for a vehicle that does not have ODB. */ #includeLiquidCrystal lcd(12, 11, 5, 4, 3, 2); // set up the LCD's number of columns and rows // constants const int sensorPin = 8; // the number of the Hall sensor pin (currently a pushbutton) const float distance = 23.5; // distance covered by the vehicle for every revolution of the drive line // variables: int sensorState = 0; // variable for reading the pushbutton status int old_sensorState = 0; // variable for pervious pushbutton status int pulse = 0; // pulse int mph = 0; // miles per hour float vlt = 0; // system voltage int psi = 0; // oil pressure in PSI int fwt = 0; // water temp in degrees F void setup() { pinMode(sensorPin, INPUT); // initialize the sensor pin as an input // Once testing is done, the Serial bits will be removed Serial.begin(9600); while (! Serial); // Wait untilSerial is ready Serial.println("Ready"); lcd.begin(16, 2); // Print a message to the LCD. lcd.print("Initializing..."); delay(2000); // wait two seconds lcd.clear(); // clear the LCD lcd.setCursor(4, 0); // move cursor to column 4 on line 0 lcd.print(" VLT"); // write VLT lcd.setCursor(4, 1); // move cursor to column 4 on line 1 lcd.print(" PSI"); // write PSI lcd.setCursor(12, 0); // move cursor to column 12 on line 0 lcd.print(" "); lcd.print((char)223); // write the degree symbol (ascii 223) lcd.print("WT"); // write WT lcd.setCursor(12, 1); // move cursor to column 12 on line 1 lcd.print(" MPH"); // write MPH } void loop() { // measure system voltage vlt = random(80, 147 )/10.00; // fake it for now // end measure system voltage // measure the water temp fwt = random(97, 195 ); // fake it for now // end measure water temp // measure the oil pressure psi = random(6, 12 ); // fake it for now // end measure oil pressure // measure vehicle speed unsigned long startTime = millis(); //get the millisecond count at the start of the loop unsigned long interval = 1000UL; // set the loop interva1 to 1 second pulse=0; // reset the number of pulses before the next count while(millis() - startTime < interval) //loop for the amount of time set by interval { sensorState = digitalRead(sensorPin); // read the state of the sensor if ((sensorState == HIGH) && (old_sensorState == LOW)) { // If the state has just changed from low to high pulse = pulse + 1; // then count a pulse delay(10); // and delay for just a bit } old_sensorState=sensorState; //set the old sensorState to the current sensorState } mph = ((pulse * distance)/(interval/1000)) * .0568; //calculate MPH // Once testing is done, the Serial bits will be removed Serial.print("Pulses = "); Serial.println(pulse); Serial.print("MPH = "); Serial.println(mph); Serial.println(vlt, 1); // end measure vehicle speed // update the display lcd.leftToRight(); // make sure the text is going the right way // display voltage lcd.setCursor(0, 0); if (vlt<10){ lcd.print(" "); // if the voltage is less than ten, blank the tens column } lcd.print(vlt, 1); // display voltage, to one decimal place // display water temp lcd.setCursor(9, 0); if (fwt<100){ lcd.print(" "); // if the temp is less than a hundred, blank the hundreds column } lcd.print(fwt); // display water temp // display oil pressure lcd.setCursor(1, 1); if (psi<100){ lcd.print(" "); // if the pressure is less than a hundred, blank the hundreds column } // (if it's more than 100PSI, something is probably broken) if (psi<10){ lcd.print(" "); // if the pressure is less than ten, blank the tens column } lcd.print(psi); // display oil pressure // display speed lcd.setCursor(9, 1); if (mph<100){ lcd.print(" "); // if the speed is less than a hundred, blank the hundreds column } // but still, don't speed. if (mph<10){ lcd.print(" "); // if the speed is less than ten, blank the tens column } lcd.print(mph); // display speed }