Digital speedometer proofread

I asked chatGPT to code a sketch for me to function as a digital speedometer.

It's failing to read the VSS signal from my 98 jeep cherokee XJ (4.0, 3 speed auto), though I'm fairly sure i have the right wire from the harness. (still have yet to do a continuity test*)

everything looks good as far as i can tell, I'm pretty good at arduino coding but I'd like a second set of eyes to make sure I'm correct.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define PULSE_PIN 2 // The pin number where the pulse sensor is connected
#define PULSES_PER_MILE 8000 // The number of pulses per mile for your speedometer sensor

// LCD settings
#define LCD_ADDRESS 0x27
#define LCD_ROWS 2
#define LCD_COLUMNS 16

LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_ROWS, LCD_COLUMNS);

void setup() {
  pinMode(PULSE_PIN, INPUT);
  lcd.init();

byte dwn[] = {
  B10001,
  B01010,
  B00100,
  B10001,
  B01010,
  B00100,
  B10001,
  B01010
};

byte up[] = {
  B01010,
  B10001,
  B00100,
  B01010,
  B10001,
  B00100,
  B01010,
  B10001
};

byte arrw[] = {
  B00100,
  B00100,
  B00100,
  B00100,
  B10101,
  B10101,
  B01110,
  B00100
};

  lcd.createChar(1, dwn);
  lcd.createChar(2, up);
  lcd.createChar(3, arrw);

  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print((char)1);
  lcd.print((char)2);
  lcd.print((char)1);
  lcd.print("  SKRRRR  ");
  lcd.print((char)2);
  lcd.print((char)1);
  lcd.print((char)2);
}

void loop() {
  unsigned long start_time = millis();
  unsigned long pulse_count = 0;
  while (millis() - start_time < 1000) {
    if (digitalRead(PULSE_PIN) == HIGH) {
      pulse_count++;
    }
  }
  float speed_mph = pulse_count * 3600.0 / PULSES_PER_MILE;
  lcd.setCursor(0, 1);
  lcd.print("Speed: ");
  lcd.print(speed_mph);
  lcd.print(" mph    ");
  delay(1000);
}

Any help is greatly appreciated, apologies for any errors in categorization or forum etiquette this is my first forum post here.

*Update: Did a continuity test, definitely have the right wire.

Update 2:
You know I've had better experiences asking reddit, for help, now that says more about this forum than I could in 1000 words.

That being said, many thanks to groundFungus for actually being helpful with no judgement or grandstanding.

As a joke or experiment?

You do realise ChatGPT doesn't understand what you want to make, or what a Jeep is, or....

It's quite convincing as long as you don't actually inspect anything it produces closely.

1 Like

An experiment mostly.

So what part of this code isn't solid?

It even did the research to find the pulses per mile of the XJ, seems pretty proficient to me.

A classic beginner error. ChatGPT is a beginner. It sounds convincing, but it's just as much a beginner as you are. Sorry if I'm offending you, but...

1 Like

I do not think that the line above is counting pulses. If a digitalRead takes 10us what will pulse_count be after PULSE_PIN is high for 1ms. I will not be one.

If you are counting pulses, you need to look for transitions not levels. So when the pin becomes high, not when it is high.

Look at the state change detection example.

I put little trust into chat_whatever or simulators.

2 Likes

Putting me off ever posting on these forums again, but not really offending me lol.

I'm not really a beginner, but I basically taught myself to code, and haven't done a project like this ever.

Thanks for actually being helpful lol.

How would you define a beginner?

ChatGPT tricked you by calling a variable pulse_count when the program it wrote is not actually counting pulses. The part that is supposed to compute and display MPH is the part that's not solid. The rest is okay.

If you were doing 100MPH that would be 800000 pulses per hour, or divided by 3600 seconds per hour, 222.222222222 per second.
Just polling the input pin for transitions would probably work.

1 Like

Well if you're asking for a resume:

I coded a custom stepper controller from scratch.

Coded an environmental computer for my jeep, with volatile compound detection.

Coded an emf detector and put in an old CD gieger counter case.

I've done 12+ projects.

So, that's what i would define as intermediately experienced.

Again not sure why this is necessary but apparently there's a degree of elitism on this forum.

Thank you, I'm in the midst of rewriting that part of the code now, also planning to use micros instead of millis.

And yet you didn't notice the obvious error in ChatGPT's code.

1 Like

Are you planning to use micros to improve something? your input pulse rate will only give you about 0.5 MPH resolution no matter how accurately you measure the one second interval.

It's been years since i coded anything, and I've never tried to count pulses like this, so no, i didn't.

Genuinely curious, what do you get out of demeaning someone here to learn instead of helping them learn?

What did ChatGPT teach you apart from "double check with a human" ?

It taught me that while AI may fall short in certain areas, its still more helpful than some arduino forum users.

Pointing out that you have an overinflated opinion of your abilities is not demeaning, it's a reality check. Maybe you learned that ChatGPT is not a particularly good resource if you are not a semi-skilled programmer.

1 Like

What exactly is productive about this discourse?

I understand that chatGPT isn't a coding expert, nor am I.

But don't assume my skill level, like I said, I'm self taught, and most of my work hasn't involved anything like this.

Seriously, what do you get out of this?
What do you hope to teach me with this?

It's pulse counting.
So, what defines a pulse?
Not seeing how much time the pin is HIGH