Recreating hall sensor signals

Hello,

For a project i am recreating hall sensor signals of a three cilinder engine. I thought i share it with you guys and maybe you can give me some pointers in the right direction?

The code is in the attachment

  int sensorValue = analogRead(A0);

This is just stupid. There is NOTHING in this code that indicates what you are reading. First thing you need to do is assign meaningful names to the variables AND the pins.

The crankshaft has 33 thooths and 2 missing.

Are you sure? There seem to be 34 crank pulses, where a pulse is a H/L cycle

I think the math works out if the crank is 34 pulses + 2 gap = 36 total counts

and the cam pulse is H for 22 cycles and L for 2 =24 total counts

Then two crank cycles = three cam cycles = 72 counts

If you count from 1-72 instead of 0-71, I think the timing looks like this

CRANK counts 1 and 2 LOW, alternating H/L for counts 3 through 36. LOW for counts 37 and 38, then alternating H/L for counts 39 through 72.

CAM counts 1 and 2 LOW, the HIGH for counts 3 through 24, LOW counts 25 and 26, HIGH for counts 27 through 48, LOW counts 49 and 50, HIGH for 51 through 72.

Is this for a 3-, 6- or 12-cylinder engine? Otherwise, the "cam" timing makes no sense at all...

Regards,
Ray L.

cattledog:
Are you sure? There seem to be 34 crank pulses, where a pulse is a H/L cycle

I think the math works out if the crank is 34 pulses + 2 gap = 36 total counts

and the cam pulse is H for 22 cycles and L for 2 =24 total counts

Then two crank cycles = three cam cycles = 72 counts

If you count from 1-72 instead of 0-71, I think the timing looks like this

CRANK counts 1 and 2 LOW, alternating H/L for counts 3 through 36. LOW for counts 37 and 38, then alternating H/L for counts 39 through 72.

CAM counts 1 and 2 LOW, the HIGH for counts 3 through 24, LOW counts 25 and 26, HIGH for counts 27 through 48, LOW counts 49 and 50, HIGH for 51 through 72.

You are right. i fixed it in the code below:

RayLivingston:
Is this for a 3-, 6- or 12-cylinder engine? Otherwise, the “cam” timing makes no sense at all…

Regards,
Ray L.

It’s a 3 cilinder engine.

This is my current code:

void setup() {                
  // initialize the digital pin as an output.

  pinMode(7, OUTPUT); //crank
  pinMode(8, OUTPUT); //cam
}

void loop(){
int val = analogRead(A0);   //read analog value
  val = map(val, 0, 1023, 800, 1000); //map analog value
 

  for (int Count = 1; Count <= 72; Count++){
     
//CRANK SIGNAL
      
      if (Count == 1){ 
      digitalWrite (7, LOW);}
      
      else if (Count == 2){ 
      digitalWrite (7, LOW);}

      else if (7, HIGH){  
      digitalWrite (7, LOW);}
    
      else if (7, LOW){ 
      digitalWrite (7, HIGH);}    

       if (Count == 37){ 
      digitalWrite (7, LOW);}
      
      else if (Count == 38){ 
      digitalWrite (7, LOW);}
      
      else if (7, HIGH){  
      digitalWrite (7, LOW);}
    
      else if (7, LOW){ 
      digitalWrite (7, HIGH);}   
      
//CAM SIGNAL
      else if (Count == 1){ 
      digitalWrite (8, LOW);}
      
      else if (Count == 2){ 
      digitalWrite (8, LOW);}

      else if (Count <= 24){ 
      digitalWrite (8, HIGH);}

      else if (Count == 25){ 
      digitalWrite (8, LOW);}
      
      else if (Count == 26){ 
      digitalWrite (8, LOW);}

      else if (Count <= 48){ 
      digitalWrite (8, HIGH);}

      else if (Count == 49){ 
      digitalWrite (8, LOW);}
      
      else if (Count == 50){ 
      digitalWrite (8, LOW);}

      else if (Count <= 72){ 
      digitalWrite (8, HIGH);}
  }
}

But somehow this is not giving the right result, when i measure this with a scope nothing happens.

PaulS:

  int sensorValue = analogRead(A0);

This is just stupid. There is NOTHING in this code that indicates what you are reading. First thing you need to do is assign meaningful names to the variables AND the pins.

You are right, I im still trying to figure this out, thank you.

What do you imagine this does?

else if (7, HIGH){

Hint: Whatever you think it does, is NOT what it actually does. The very first one is effectively simply an else, and all the other else's will never be executed...

Regards,
Ray L.

....

So.... When you want to set a pin HIGH, you use:

digitalWrite(7, HIGH);

What would make you think you can test the state of a pin by just doing:

(7, HIGH)

You've certainly never seen a working example that did that. How could the compiler possibly know "(7, HIGH) means "read pin 7, and compare it's value to the value of HIGH, and return the boolean result of that comparison". To the compiler, 7, and HIGH, are both simply numbers. In fact, what the compiler actually sees is "(7, 1)", which is basically gibberish, albeit gibberish with valid c/c++ syntax which evaluates to simply "7". The compiler sees nothing about reading input pins, or doing logical, or numeric, comparisons.
Read up on digitalRead()
If you're going to generate a waveform based on a counter, you should be using a switch statement, rather than a pile of if/else statements. Also, what you're doing has NO defined timing. The pulses will have uncontrolled jitter, and a somewhat random overall cycle period.
Regards,
Ray L.

I’m not smart enough to figure out short cuts, but I know what has to happen in each of 72 switch case statements. But, I don’t like to type either, so here is a small sketch to run which will generate 72 switch case statements. Fill in the blanks when you paste the serial monitor printout into your sketch. # define crank 7 and cam 8 at the start of the program

There are a few cases with no transitions, so you can just use break in them.

void setup() {
  Serial.begin(115200);
  for (int i = 1; i<=72; i++)
  {
    Serial.print("Case ");
    Serial.print(i);
    Serial.println(":");
    Serial.println("digitalWrite(crank, );");
    Serial.println("digitalWrite(cam, );");
    Serial.println("break;");
    Serial.println();
  }   
}

void loop() {
  // put your main code here, to run repeatedly:

}

,,,

Can you please clean up your code by removing the unused portions and formatting the code with the auto format tool in the ide or with the keyboard use of (Ctrl +T). If it’s still too large to post, attach it in the “Reply” window, and don’t use the “Quick Reply” window.

There are a few little pieces of clean up that I see like Case 2,3,and 4 an simply consist of break; as there are no changes to the pin outputs.

If timing is critical, you can replace all the digitalWrite() statements with the direct port commands Arduino - PortManipulation

How did things change from 72 counts to 144 counts?