Analog Read how to do it?

This code is correct and works but I need instead of calcFactor change I can do it analog with potentiometer and change to A0 (Analog read) how to do it?

//1986 Porsche 944 Turbo Speedometer Calibrator

//Public Domain

//raw 944 transmission sensor signal must be processed by external schmitt trigger

//Schmitt trigger should bypass filer and pull-up resistor

//Use LM2940-10 with decoupling caps per datasheet to power Arduino in automotive setting

//Arduino Pin 7 supplies modified signal for speedometer

const int debounce = 2500;

const int speedometerPin = 7;

const int sensorPin = 3;

int pulseState = LOW;

volatile unsigned long currentMicros = 0;

volatile unsigned long previousMicros= 0;

volatile unsigned long currentSpeed = 0;

volatile unsigned long previousSpeed = 0;

volatile unsigned long interval = 0;

unsigned long modInterval = 0;

float calFactor = .91; // decrease to slow down speedometer

// calFactor of 1 makes no change to speedometer

void setup()

{

pinMode (13, OUTPUT);

pinMode(speedometerPin, OUTPUT);

pinMode(sensorPin, INPUT);

digitalWrite (sensorPin, HIGH);

attachInterrupt (1, iSr, FALLING);

}
void loop()

{

noInterrupts();

modInterval=interval;

interrupts();

currentMicros = micros();

if (currentMicros-previousSpeed<1000000)

{

if (currentMicros - previousMicros>((modInterval/2)/calFactor))

{ previousMicros = currentMicros;

if (pulseState == LOW) pulseState = HIGH; else pulseState = LOW;

digitalWrite(13, pulseState); //to blink onboard LED

digitalWrite(speedometerPin, pulseState);

}

}

}

void iSr()

{

currentSpeed=micros();

if (digitalRead(sensorPin)==LOW)

{

if ((currentSpeed - previousSpeed) > debounce)

{

interval = currentSpeed - previousSpeed;

previousSpeed=currentSpeed;

}

}

}

Try by changing this line: if (currentMicros - previousMicros>((modInterval/2)/calFactor)) to the following:

if (currentMicros - previousMicros>((modInterval/2)/(analogRead(A0)/1000.0))  //0.000 - 1.023

Test Codes:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  float calFactor = analogRead(A0)/1000.0;
  Serial.println(calFactor, 3); //connect A0-pin at 3.3V of UNO; Serial Monitor shows 0.667
  delay(1000);
}

And always use code tags (</>) when posting codes and the tips are:
1. Click on this icon </> of the toolbar of the posting window.
2. Copy your codes from the IDE.
3. Come back to this window and be sure you have the cursor blinking.
4. Just press Ctrl-V.

Thanks so much for the speedy answer did you think of any such code changes?

//1986 Porsche 944 Turbo Speedometer Calibrator

//Public Domain

//raw 944 transmission sensor signal must be processed by external schmitt trigger

//Schmitt trigger should bypass filer and pull-up resistor

//Use LM2940-10 with decoupling caps per datasheet to power Arduino in automotive setting

//Arduino Pin 7 supplies modified signal for speedometer

const int debounce = 2500;

const int speedometerPin = 7;

const int sensorPin = 3;

int pulseState = LOW;

volatile unsigned long currentMicros = 0;

volatile unsigned long previousMicros= 0;

volatile unsigned long currentSpeed = 0;

volatile unsigned long previousSpeed = 0;

volatile unsigned long interval = 0;

unsigned long modInterval = 0;

float calFactor = .91; // decrease to slow down speedometer

// calFactor of 1 makes no change to speedometer

if (currentMicros - previousMicros>((modInterval/2)/(analogRead(A0)/1000.0)) //0.000 - 1.023

void setup()
{
Serial.begin(9600);
}

void loop()
{
float calFactor = analogRead(A0)/1000.0;
Serial.println(calFactor, 3); //connect A0-pin at 3.3V of UNO; Serial Monitor shows 0.667
delay(1000);
}

void loop()

{

noInterrupts();

modInterval=interval;

interrupts();

currentMicros = micros();

if (currentMicros-previousSpeed<1000000)

{

{ previousMicros = currentMicros;

if (pulseState == LOW) pulseState = HIGH; else pulseState = LOW;

digitalWrite(13, pulseState); //to blink onboard LED

digitalWrite(speedometerPin, pulseState);

}

}

}

void iSr()

{

currentSpeed=micros();

if (digitalRead(sensorPin)==LOW)

{

if ((currentSpeed - previousSpeed) > debounce)

{

interval = currentSpeed - previousSpeed;

previousSpeed=currentSpeed;

}

}

}

Why have you not used the code tags (</>) to post your codes?

Why have you taken out this line: if (currentMicros - previousMicros>((modInterval/2)/(analogRead(A0)/1000.0)) //0.000 - 1.023 from the loop() function? It should be there where it was originally.

const int debounce = 2500;
const int speedometerPin = 7;
const int sensorPin = 3;
int pulseState = LOW;
volatile unsigned long currentMicros = 0;
volatile unsigned long previousMicros = 0;
volatile unsigned long currentSpeed = 0;
volatile unsigned long previousSpeed = 0;
volatile unsigned long interval = 0;
unsigned long modInterval = 0;
//float calFactor = .91;   // decrease to slow down speedometer
// calFactor of 1 makes no change to speedometer

void setup()
{
  pinMode (13, OUTPUT);
  pinMode(speedometerPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  digitalWrite (sensorPin, HIGH);
  attachInterrupt (1, iSr, FALLING);
}

void loop()
{
  noInterrupts();
  modInterval = interval;
  interrupts();
  currentMicros = micros();
  if (currentMicros - previousSpeed < 1000000)
  {
    if (currentMicros - previousMicros > ((modInterval / 2) / (analogRead(A0) / 1000.0))) //calFactor))
    {
      previousMicros = currentMicros;
      if (pulseState == LOW)
        pulseState = HIGH;
      else
        pulseState = LOW;
      digitalWrite(13, pulseState);         //to blink onboard LED
      digitalWrite(speedometerPin, pulseState);
    }
  }
}

void iSr()
{
  currentSpeed = micros();
  if (digitalRead(sensorPin) == LOW)
  {
    if ((currentSpeed - previousSpeed) > debounce)
    {
      interval = currentSpeed - previousSpeed;
      previousSpeed = currentSpeed;
    }
  }
}

I tried the circuit and it changes the frequency from 1-99% to 250Hz for the frequency of frequency does not go up to max 30%?

pajper:
I tried the circuit and it changes the frequency from 1-99% to 250Hz for the frequency of frequency does not go up to max 30%?

Welcome to the Forum. You have now been asked twice, to correct a problem in your post. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

It is important to provide as much of the information that is needed to solve your problem as you can, in your first posts. The forum link above has guidelines for posting in a standard way that makes it easiest for people to provide you with useful answers. Making an effort to do this will greatly increase the number and quality of helpful responses that you get.

In this case, the problem is that you have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Thanks for the welcome this is when I think the signs are honored and I say does it or not for the full frequency range?

const int debounce = 2500;
const int speedometerPin = 7;
const int sensorPin = 3;
int pulseState = LOW;
volatile unsigned long currentMicros = 0;
volatile unsigned long previousMicros = 0;
volatile unsigned long currentSpeed = 0;
volatile unsigned long previousSpeed = 0;
volatile unsigned long interval = 0;
unsigned long modInterval = 0;
//float calFactor = .91; // decrease to slow down speedometer
// calFactor of 1 makes no change to speedometer

void setup()
{
pinMode (13, OUTPUT);
pinMode(speedometerPin, OUTPUT);
pinMode(sensorPin, INPUT);
digitalWrite (sensorPin, HIGH);
attachInterrupt (1, iSr, FALLING);
}

void loop()
{
noInterrupts();
modInterval = interval;
interrupts();
currentMicros = micros();
if (currentMicros - previousSpeed < 1000000)
{
if (currentMicros - previousMicros > ((modInterval / 2) / (analogRead(A0) / 1000.0))) //calFactor))
{
previousMicros = currentMicros;
if (pulseState == LOW)
pulseState = HIGH;
else
pulseState = LOW;
digitalWrite(13, pulseState); //to blink onboard LED
digitalWrite(speedometerPin, pulseState);
}
}
}

void iSr()
{
currentSpeed = micros();
if (digitalRead(sensorPin) == LOW)
{
if ((currentSpeed - previousSpeed) > debounce)
{
interval = currentSpeed - previousSpeed;
previousSpeed = currentSpeed;
}
}
}

If you expect to receive any answer, please carry out the following steps to post your codes using code tags (</>). You are violating the Forum Rules in spite of repeated requests for using code tags while posting codes.

And always use code tags (</>) when posting codes and the tips are:
1. Click on this icon </> of the toolbar of the posting window.
2. Copy your codes from the IDE.
3. Come back to this window and be sure you have the cursor blinking.
4. Just press Ctrl-V.