# Modifying Code Found from elsewhere

Hello, im trying to modify the square wave output from my speed sensor on my car, i have put larger wheels on so my speed reads slow, i found an old forum (2015) that someone had done exactly that just for the Tachometer instead, i have tried a few things to correct the code but it only seems to multiply it by 2,

Im trying to import a frequency, Multiply it by a 9% then output it on a seperate pin. I managed to use another arduino to produce a square wave so i have a baseline figure that can easily change. but just cant get my head around the import export code.

If anyone can see the problem with the attached code, any help will be appreciated!!!

``````// Pulse Doubler
// Public Domain  Paul B.

#define OUT_PIN 13
#define IN_PIN 7

// Concept: Look for transitions; need state memory
byte in_was = LOW;
// Read once per cycle
byte in_now;
// Time of last transition up
unsigned long wentup = 0;
// Time of last transition down
unsigned long wentdn = 0;
// Output pulse duration
unsigned long pulsetime = 0;
// Time last pulse started
unsigned long pulseis = 0;

// Initiate an output pulse
void pulseon(unsigned long length) {
digitalWrite(OUT_PIN, HIGH);
pulsetime = length >> 2;  // A quarter of the last interval
pulseis = micros();
}

void setup(){
pinMode(OUT_PIN, OUTPUT);
pinMode(IN_PIN, INPUT);
digitalWrite(IN_PIN, HIGH);  // Input pullup
}

void loop()
{
// Read pin once
// Now, were we high or low?
if (in_was == LOW) {
if (in_now == HIGH) {
// So, has transitioned from low to high
in_was = HIGH;
pulseon(micros() - wentup);
wentup = micros();
}
}
else {
if (in_now == LOW) {
// So, has transitioned from high to low
in_was = LOW;
pulseon(micros() - wentdn);
wentdn = micros();
}
}

delay(15); // Very crude debounce for proof of concept only
// Now perform the pulse delay
if (pulsetime != 0) {
if (micros() - pulseis >= pulsetime) {
pulsetime = 0;
digitalWrite(OUT_PIN, LOW);
}
}
}
``````

robbie98:
....Im trying to import a frequency, Multiply it by a 9% then output it on a seperate pin....

assuming you mean that you wish to increase the the frequency by 9% then IMHO you should change the following line in the 'pulseon' routine

``````pulsetime = length >> 2; // A quarter of the last interval
``````

to

``````unsigned long long temp = (length*100)/109; //using 'unsigned long long' to avoid risk of overflow when calculating new time period
pulsetime = (unsigned long)temp; //frequency increased by 9%
``````

hope that helps....

Im trying to import a frequency, Multiply it by a 9% then output it on a seperate pin.

looks like the code isn't generating a pulse properly. can't used delay because it affects the timing of the generated pulse.

``````// pulse rate converter

#define OUT_PIN 10
#define IN_PIN  A1

// -----------------------------------------------------------------------------
// toggle pin every half period
void
pulse (
unsigned long period,
byte          pin )
{
static unsigned long usecLst = 0;
unsigned long usec    = micros ();

if (usec - usecLst > (period / 2))  {
usecLst = usec;
digitalWrite (pin, ! digitalRead (pin));
}
}

// -----------------------------------------------------------------------------
void
loop (void)
{
static unsigned long period1 = 0;
static unsigned long period2 = 100000;

// -------------------------------------
// generate output pulse
pulse (period2, OUT_PIN);

// -------------------------------------
// measure input pin period
static unsigned long usecLst = 0;
unsigned long usec    = micros ();

static unsigned long pinUsecLst  = 0;
static byte          pinStateLst = 0;
byte          pinState;

pinState = digitalRead (IN_PIN);

// can't use delay to debounce, affects pulse() timing
#define K               2
#define DEBOUNCE_MSEC   10
if (pinStateLst != pinState && (usec - usecLst > DEBOUNCE_MSEC)) {
pinStateLst = pinState;
usecLst     = usec;

// measure time since last rising edge
if (HIGH == pinState)  {
period1    = usec - pinUsecLst;
period2    = K * period1;

char s [40];
sprintf (s, " %8ld %8ld %8ld %8ld",
period1, period2, usec, pinUsecLst);
Serial.println (s);

pinUsecLst = usec;
}
}
}

// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (115200);

pinMode (OUT_PIN, OUTPUT);
pinMode (IN_PIN, INPUT);
}
``````