Help needed, crank signal generator. <solved>

User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

Sending the number 20000 gets you about 360 RPM, while sending 1 gets you 100,876 RPM.
The number get's multiplied by a small number and that result is microseconds between each rise and fall of the crank signal.
Trying to do any serial monitoring gives a garbage signal (I don't yet want to try multi-threaded coding).
The signal pauses very briefly after sending each value. Like zoomed way in on the oscilloscope brief.

Code: Select all

int TPS = 1;
int sequence[48] = {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0};
int cam[48] =      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int phase = 0;
int tooth = 0;
int low = 0;
int high = 0;
int RPMlow = 0;
int RPMhigh = 0;
int math = 1;
int halfdone = 1;
int done = 1;
int width = 1;
unsigned long previousmicros = 0;
int Hz = 1;
void setup() {
Serial.begin(300);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);

}

void loop() {
if (Serial.available())
{
 Hz = Serial.parseInt();
}

if (halfdone == 0) {
  if (micros() - previousmicros >= RPMlow) {
      halfdone = 1;
      done = 0;
      digitalWrite(0, HIGH);
      previousmicros = micros();
    }
  }
if (done == 0) {
  if (micros() - previousmicros >= RPMhigh) {
      done = 1;
      math = 1;
      digitalWrite(0, LOW);
      previousmicros = micros();
    }
  }
   
if (math == 1) {
tooth++;
if (tooth == 48) {
  tooth = 0;
  }

TPS = Hz; 
width = sequence[tooth];
phase = cam[tooth];
digitalWrite(1, phase);
if (width == 0) {
  low = 2;
  high = 1; 
  }
if (width == 1) {
    low = 1;
    high = 2;
  }
halfdone = 0;
math = 0;
RPMlow = low * TPS;
RPMhigh = high * TPS;

} 
}

Last edited by AngelMarc on Sun Apr 27, 2025 2:22 pm, edited 1 time in total.
Don't stress specific units.
User avatar
antus
Site Admin
Posts: 8987
Joined: Sat Feb 28, 2009 8:34 pm
cars: TX Gemini 2L Twincam
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Contact:

Re: Help needed, crank signal generator. <solved>

Post by antus »

I know you don't care about the serial for now, but my experience with that is that some devices connect serial in hardware out of the box, some don't. It's to do with the USB connection or what not. For this board, if you wanted to use it, you'd probably need to do some configuration to enable a hardware uart, and then connect those pins to the PC through some kind of USB to TTL adapter. Having said that, there may also be some way to configure a uart that is connected by default, presumably through the usb interface I would guess you have connected to the computer.
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

It's reading serial data I send it. The other way is... breaks it. Cam is added already.
See some details here. Original post has most recent code.
https://www.youtube.com/watch?v=EXo97nUB3hk
I need to get another soldering iron to use a TC4452V to up the voltage (and current if that's even needed), before I can test with the ECU.
Last edited by AngelMarc on Sun Apr 27, 2025 11:47 am, edited 1 time in total.
Don't stress specific units.
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator.

Post by AngelMarc »

antus wrote: Sat Apr 26, 2025 3:01 pm Then I realized, I don't think there is any code to read a timer so currentmillis is always 0
And about that. Arduino has some default commands like millis and micros that it... it does some things for us in the background. There's a timer.
Don't stress specific units.
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

This line was the eureka moment.

Code: Select all

width = sequence[tooth];
Instead of just putting "sequence[tooth]" where "width" is. Don't know why it wouldn't work the first way, but whatever, it's fixed.

Code: Select all

width = sequence[tooth];
phase = cam[tooth];
digitalWrite(1, phase);
if (width == 0) {
  low = 2;
  high = 1; 
  }
if (width == 1) {
    low = 1;
    high = 2;
  }
Don't stress specific units.
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

Was going to be a bro and throw in a 58x but... bugs.

Code: Select all

int TPS = 777;
int sequence[120] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0};
int cam[120] =      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int phase = 0;
int tooth = 0;
int low = 0;
int high = 0;
int RPMlow = 0;
int RPMhigh = 0;
int math = 1;
int halfdone = 1;
int done = 1;
int width = 1;
unsigned long previousmicros = 0;
void setup() {
Serial.begin(300);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);

}

void loop() {
if (Serial.available())
{
 TPS = Serial.parseInt();
}

if (halfdone == 0) {
  if (micros() - previousmicros >= RPMlow) {
      halfdone = 1;
      done = 0;
      digitalWrite(0, HIGH);
      previousmicros = micros();
    }
  }
if (done == 0) {
  if (micros() - previousmicros >= RPMhigh) {
      done = 1;
      math = 1;
      digitalWrite(0, LOW);
      previousmicros = micros();
    }
  }
   
if (math == 1) {
tooth++;
if (tooth == 120) {
  tooth = 0;
  }
 
width = sequence[tooth];
phase = cam[tooth];
digitalWrite(1, phase);
if (width == 0) {
  low = 2;
  high = 0; 
  }
if (width == 1) {
    low = 1;
    high = 1;
  }
halfdone = 0;
math = 0;
RPMlow = low * TPS;
RPMhigh = high * TPS;

} 
}

Attachments
Capture.PNG
Capture.PNG (18.86 KiB) Viewed 3839 times
Don't stress specific units.
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

antus wrote: Sun Apr 27, 2025 11:06 am I know you don't care about the serial for now, but my experience with that is that some devices connect serial in hardware out of the box, some don't. It's to do with the USB connection or what not. For this board, if you wanted to use it, you'd probably need to do some configuration to enable a hardware uart, and then connect those pins to the PC through some kind of USB to TTL adapter. Having said that, there may also be some way to configure a uart that is connected by default, presumably through the usb interface I would guess you have connected to the computer.
Common for Arduinos to have that baked in with the board files and what not.
Don't stress specific units.
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

Tested on a P59 over 9,000 RPM scanner disconnects much beyond that.
https://www.youtube.com/watch?v=e32bYJsmmgw
Don't stress specific units.
User avatar
antus
Site Admin
Posts: 8987
Joined: Sat Feb 28, 2009 8:34 pm
cars: TX Gemini 2L Twincam
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Contact:

Re: Help needed, crank signal generator. <solved>

Post by antus »

Looks good :thumbup:
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
AngelMarc
Posts: 198
Joined: Sat Apr 08, 2023 9:23 pm
cars: A CB450 running to 8,000RPM with a P59.

Re: Help needed, crank signal generator. <solved>

Post by AngelMarc »

Thanks. I'm excited to contribute how I can. I'd do it for myself anyway. So... Here you go, everybody else; copy paste.
I think this is the 1st thing I've coded without copy pasting chunks of other people's code.
Don't stress specific units.
Post Reply