Programming problem need HELP

Hi can anyone help with this sketch, it is supposed to read ground speed radar, and shaft rpm with a reed switch, and control a servo to best match shaft speed to ground speed. i can get the inputs reading well by themselves in separate sketches but when i try to combine them they don’t read at all, im guessing its a problem with the way i’m asking the arduino to follow my programming, please have a look and any help would be appreciated

#include <Servo.h>

int kmph = 0;
int sharftHz = 0;
int sharftRpm =0;
int requiredSharftRpm = 0;

const int frqPin = 5; //Input Pin
const int oneSecond = 100; //Meassured time
const int sharftHallsenserPin = 2; //Input Pin

Servo myservo;

int frqPinState = LOW;
int frq = 0;
int prevFrqPinState = LOW;
unsigned long timerA = 0;

int sharftHallsenserState = LOW;
int prevsharftHallsenserState = LOW;
int pos = 15;
int val; // variable to read the value from the analog pin
unsigned long timerB = 0;

void setup()
{Serial.begin(9600);
pinMode(frqPin, INPUT); //Declared Pin 5 as Input
pinMode(sharftHallsenserPin, INPUT); //Declared Pin 2 as Input
myservo.attach(9);
pinMode(13, OUTPUT);}

void loop()
{ //program

{//flash red LED
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13,LOW);
}

//ground radar frequency to km/h
{ frqPinState = digitalRead(frqPin); //Reads State of Input Pin

if (frqPinState == LOW) //Creates a previos state
{prevFrqPinState = LOW;}

if (frqPinState == HIGH && prevFrqPinState == LOW) //Pulses Counter
{prevFrqPinState = frqPinState;
frq++;}

if (millis() - timerA > oneSecond) //Time Counter
{timerA = millis(); //Resets Time
frq = 0; //Resets Frequency
}
{
Serial.print("Ground Speed Frq: ");
Serial.println(frq); //Prints Frequency
kmph = ((frq / 15.6)+0);
Serial.print("Km/h: ");
Serial.println(kmph);}
delay(100);
}

//Calulate sharft Frequency to rpm

{sharftHallsenserState = digitalRead(sharftHallsenserPin);
if (sharftHallsenserState ==LOW)

{prevsharftHallsenserState = LOW;}

if (sharftHallsenserState == HIGH && prevsharftHallsenserState == LOW) //Pulses Counter
{prevsharftHallsenserState = sharftHallsenserState;
sharftHz++;}

if (millis() - timerB > oneSecond) //Time Counter
{timerB = millis(); //Resets Time
sharftHz = 0; //Resets Frequency

Serial.print("Sharft Hz: ");
Serial.println(sharftHz);}
sharftRpm = (sharftHz * 60 / 4);
Serial.print("Sharft Rpm: ");
Serial.println(sharftRpm);
delay(100);
}

//calculate required sharft speed and send adjustment to servo
{requiredSharftRpm = kmph * 12.45;
{
Serial.print("Required Sharft Speed: ");
Serial.println(requiredSharftRpm);
}

if (sharftRpm > requiredSharftRpm)
{ pos = pos - 2;
pos = max(pos, 2); // assigns pos to the larger of pos or 2

myservo.write(pos); // tell servo to go to position in variable ‘pos’
Serial.print("Servo Location: ");
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
else
{ Serial.print("No Gain: ");
}

if (sharftRpm < requiredSharftRpm)
{ pos = pos + 2;
pos = min(pos,179);

myservo.write(pos); // tell servo to go to position in variable ‘pos’
Serial.print("Servo Location: ");
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
else
{ Serial.print("No Drop: ");
}
}
{
Serial.print("End Of A Cycle: ");
delay(100);
}
}

  1. Post your code in code tags,

  2. Auto format your code BEFORE posting it,

  3. At least get to compile before ask us why it's not working

  4. GET RID OF THE DELAY

Mark

Code compiles OK on IDE 1.6.4

#include <Servo.h>


int kmph = 0;
int sharftHz = 0;
int sharftRpm =0;
int requiredSharftRpm = 0;

const int frqPin = 5; //Input Pin
const int oneSecond = 100; //Meassured time
const int sharftHallsenserPin = 2; //Input Pin

Servo myservo;

int frqPinState = LOW;
int frq = 0;
int prevFrqPinState = LOW;
unsigned long timerA = 0;

int sharftHallsenserState = LOW;
int prevsharftHallsenserState = LOW;
int pos = 15;
int val;    // variable to read the value from the analog pin
unsigned long timerB = 0;

void setup()
{Serial.begin(9600);
pinMode(frqPin, INPUT); //Declared Pin 5 as Input
pinMode(sharftHallsenserPin, INPUT); //Declared Pin 2 as Input
myservo.attach(9);
pinMode(13, OUTPUT);}


void loop() 
{ //program 

{//flash red LED
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13,LOW);
}

//ground radar frequency to km/h
{ frqPinState = digitalRead(frqPin); //Reads State of Input Pin

if (frqPinState == LOW) //Creates a previos state
{prevFrqPinState = LOW;}

if (frqPinState == HIGH && prevFrqPinState == LOW) //Pulses Counter
{prevFrqPinState = frqPinState;
    frq++;}
  
if (millis() - timerA > oneSecond) //Time Counter
{timerA = millis(); //Resets Time
frq = 0; //Resets Frequency
}
 {
 Serial.print("Ground Speed Frq:  ");
 Serial.println(frq); //Prints Frequency
 kmph = ((frq / 15.6)+0);
 Serial.print("Km/h:  ");
 Serial.println(kmph);}
 delay(100);
}

//Calulate sharft Frequency to rpm

{sharftHallsenserState = digitalRead(sharftHallsenserPin);
if (sharftHallsenserState ==LOW)

{prevsharftHallsenserState = LOW;}

if (sharftHallsenserState == HIGH && prevsharftHallsenserState == LOW) //Pulses Counter
{prevsharftHallsenserState = sharftHallsenserState;
  sharftHz++;}
  
if (millis() - timerB > oneSecond) //Time Counter
{timerB = millis(); //Resets Time
  sharftHz = 0; //Resets Frequency
  
  Serial.print("Sharft Hz:  ");
  Serial.println(sharftHz);} 
  sharftRpm = (sharftHz * 60 / 4);
  Serial.print("Sharft Rpm:  ");
  Serial.println(sharftRpm);
  delay(100);
}

//calculate required sharft speed and send adjustment to servo
  {requiredSharftRpm = kmph * 12.45;
  {
    Serial.print("Required Sharft Speed:  ");
    Serial.println(requiredSharftRpm);
  }

 if (sharftRpm > requiredSharftRpm)
   { pos = pos - 2;
   pos = max(pos, 2); // assigns pos to the larger of pos or 2
                          

    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.print("Servo Location:  ");
    Serial.println(pos);
    delay(15);                       // waits 15ms for the servo to reach the position
   }
   else 
   { Serial.print("No Gain:  ");
   }
    
    
    if (sharftRpm < requiredSharftRpm)
    { pos = pos + 2;
    pos = min(pos,179);
    
      
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.print("Servo Location:  ");
    Serial.println(pos);
    delay(15);                       // waits 15ms for the servo to reach the position
}
  else
  { Serial.print("No Drop:  ");
  }
  }
{
Serial.print("End Of A Cycle:   ");
delay(100);
}
}

Sketch uses 6,170 bytes (20%) of program storage space. Maximum is 30,720 bytes.
Global variables use 395 bytes (19%) of dynamic memory, leaving 1,653 bytes for local variables. Maximum is 2,048 bytes.

Please describe what your code does (or doesn’t do).

const int oneSecond = 100; //Meassured time

;) well hidden, most readers would believe the value of oneSecond in millis() were 1000...

Concatenating different loop constructs (without even removing the output moderating delays) is obviously not the correct approach to combine them.

Whandall: const int oneSecond = 100; //Meassured time

;) well hidden, most readers would believe the value of oneSecond in millis() were 1000...

Also, the name is opaque. Conversion constants should be explicit, as in MILLISECONDS_PER_SECOND.

Whandall: const int oneSecond = 100; //Meassured time

;) well hidden, most readers would believe the value of oneSecond in millis() were 1000...

Concatenating different loop constructs (without even removing the output moderating delays) is obviously not the correct approach to combine them.

Time was 700 in original sketch just don't know how you put two or more codes together properly

lancegaskell:
Hi can anyone help with this sketch, it is supposed to read ground speed radar, and shaft rpm with a reed switch, and control a servo to best match shaft speed to ground speed. i can get the inputs reading well by themselves in separate sketches but when i try to combine them they don’t read at all, im guessing its a problem with the way i’m asking the arduino to follow my programming, please have a look and any help would be appreciated

#include <Servo.h>

#define DEBUG

int kmph = 0;
int sharftHz = 0;
int sharftRpm =0;
int requiredSharftRpm = 0;

const int frqPin = 5; //Input Pin
const int oneSecond = 100; //Meassured time
const int sharftHallsenserPin = 2; //Input Pin

Servo myservo;

int frqPinState = LOW;
int frq = 0;
int prevFrqPinState = LOW;
unsigned long timerA = 0;

int sharftHallsenserState = LOW;
int prevsharftHallsenserState = LOW;
int pos = 15;
int val; // variable to read the value from the analog pin
unsigned long timerB = 0;

void setup()
{Serial.begin(9600);
pinMode(frqPin, INPUT); //Declared Pin 5 as Input
pinMode(sharftHallsenserPin, INPUT); //Declared Pin 2 as Input
myservo.attach(9);
pinMode(13, OUTPUT);}

void loop()
{ //program

{//flash red LED
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13,LOW);
}

//ground radar frequency to km/h

{ frqPinState = digitalRead(frqPin); //Reads State of Input Pin

#ifdef DEBUG
frqPinState = 0; // initialize / force to low
#endif


if (frqPinState == LOW) //Creates a previos state
{prevFrqPinState = LOW;}

if (frqPinState == HIGH && prevFrqPinState == LOW) //Pulses Counter
{prevFrqPinState = frqPinState;
frq++;}

if (millis() - timerA > oneSecond) //Time Counter
{timerA = millis(); //Resets Time
frq = 0; //Resets Frequency
}
{
Serial.print("Ground Speed Frq: ");
Serial.println(frq); //Prints Frequency
kmph = ((frq / 15.6)+0);
Serial.print("Km/h: ");
Serial.println(kmph);}
delay(100);
}

//Calulate sharft Frequency to rpm

{sharftHallsenserState = digitalRead(sharftHallsenserPin);
if (sharftHallsenserState ==LOW)

{prevsharftHallsenserState = LOW;}

if (sharftHallsenserState == HIGH && prevsharftHallsenserState == LOW) //Pulses Counter
{prevsharftHallsenserState = sharftHallsenserState;
sharftHz++;}

if (millis() - timerB > oneSecond) //Time Counter
{timerB = millis(); //Resets Time
sharftHz = 0; //Resets Frequency

Serial.print("Sharft Hz: ");
Serial.println(sharftHz);}
sharftRpm = (sharftHz * 60 / 4);
Serial.print("Sharft Rpm: ");
Serial.println(sharftRpm);
delay(100);
}

//calculate required sharft speed and send adjustment to servo
{requiredSharftRpm = kmph * 12.45;
{
Serial.print("Required Sharft Speed: ");
Serial.println(requiredSharftRpm);
}

if (sharftRpm > requiredSharftRpm)
{ pos = pos - 2;
pos = max(pos, 2); // assigns pos to the larger of pos or 2

myservo.write(pos); // tell servo to go to position in variable ‘pos’
Serial.print("Servo Location: ");
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
else
{ Serial.print("No Gain: ");
}

if (sharftRpm < requiredSharftRpm)
{ pos = pos + 2;
pos = min(pos,179);

myservo.write(pos); // tell servo to go to position in variable ‘pos’
Serial.print("Servo Location: ");
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
else
{ Serial.print("No Drop: ");
}
}
{
Serial.print("End Of A Cycle: ");
delay(100);
}
}

Suggestion
you can beautify your code as suggested
But at this point it is irrelevant to the app function that 1 second != 100 ms or you are using delay.

Let’s get down to the business and troubleshoot.

You code actually has nice blocks which makes it very easy to identify particular functionality , no matter what it does. ( To the nosy - it checks high and low and time duh!)

So, modify the frqPinState block as follows and run it.
You should add more Serial.print to make sure the variables and the code flow is doing what is expected.
If you keep enclosing them in #ifdef DEBUG … #endif code it is easy to get rid of them when you are done.
“Rinse and repeat” ( goto TestLabel;) the test with setting the frqPinState to high ( figure out how for extra credit) , adjusting /shortening the timerA etc.

Of course you cannot run the test with all the options same time. The test must be logical or it is going to be a mess. Use delay to slow down the Serial prints to manageable / visible output.

Than do same with next functional block…

#define DEBUG 
  //ground radar frequency to km/h
  { frqPinState = digitalRead(frqPin); //Reads State of Input Pin
#ifdef DEBUG 
TestLabel: frqPinState = 0;       // test force pin state to low, loop back here after delay etc.. 
#endif

    if (frqPinState == LOW) //Creates a previos state
    {
      prevFrqPinState = LOW; // this is pretty clear - no need to check 
    }

    if (frqPinState == HIGH && prevFrqPinState == LOW) //Pulses Counter
    { 
     // this block should be bypassed - is it ???
      prevFrqPinState = frqPinState;
      frq++;
    }

   // some test delay here on next go aroudn 
    if (millis() - timerA > oneSecond) //Time Counter
    { 
    // did it get bypassed on first try ?
    
      timerA = millis(); //Resets Time
      frq = 0; //Resets Frequency
    }
    {
      Serial.print("Ground Speed Frq:  ");
      Serial.println(frq); //Prints Frequency
      kmph = ((frq / 15.6) + 0);
      Serial.print("Km/h:  ");
      Serial.println(kmph);
    }
    delay(100);

#ifdef DEBUG 
   // put delay of more than 1 s here and 
   goto TestLabel;
#endif

   
  }
  #ifdef DEBUG 
  Serial.println("frqPinState block test DONE ); //Prints Frequency
  for(;;);       // stop and analyze - did it work as excpected?
  #endif