Trouble Mapping arrayed PWM output values to specified pulse count input

I got some tips in Project guidance forum and combined a Pulse counting sketch with the suggested code tips I received .

I tested the pulse counting sketch first with out modifications and it work perfect displaying serial pulse reads 0-200hz in monitor mode .

Arduino IDE would not let me put all values desired in array so I thought I would try the 5 values it would accept .
(I got an error message saying there were too many values when I tried to include them all )

After test code was added to enable PWM output the Monitor frequency read looks accurate but the PWM pin output is 0 at all speeds and the serial display for this output looks to be jibberish .
Any suggestions on what I messed up ?

 // Frequency counter sketch, for measuring frequencies low enough to execute an interrupt for each cycle
// Connect the frequency source to the INT0 pin (digital pin 2 on an Arduino Uno)

volatile unsigned long firstPulseTime;
volatile unsigned long lastPulseTime;
volatile unsigned long numPulses;
const int PWMvalue[11] = { 0, 27, 43, 64, 85, };         // 107, 128, 148, 171, 192, 213, 235, 256 }


void isr()
{
 unsigned long now = micros();
 if (numPulses == 1)
 {
   firstPulseTime = now;
 }
 else
 {
   lastPulseTime = now;
 }
 ++numPulses;
}

void setup()
{
 Serial.begin(9600);    // this is here so that we can print the result
                               // put a PWM signal on pin 3, then we can connect pin 3 to pin 2 to test the 
                                                                              counter  removed (pinMode(3, OUTPUT);
                                 // removed   ( analogWrite(3, 128);)  INJECTING MY OWN VARIABLE 
                                           SPEED VALUE VIA OPTICAL SENSOR/MOTOR D2 pin
}

                                                 // Measure the frequency over the specified sample time in 
                                                                       milliseconds, returning the frequency in Hz
float readFrequency(unsigned int sampleTime)
{
 numPulses = 0;                      // prime the system to start a new reading
 attachInterrupt(0, isr, RISING);    // enable the interrupt
 delay(sampleTime);
 detachInterrupt(0);
 return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime);
}

void loop()
{
 float freq = readFrequency(1000);
 int bin = (freq+5/12);
  PWMvalue[bin];
 
 
 Serial.println(freq);
 delay(1000);
 Serial.println(PWMvalue[bin]);
 delay(1000);
}

Modified_Frequency_counter_sketch.ino (1.61 KB)

You messed up the text of the code :o
Some lines are too long, some are split. The sketch that you showed does not compile.

What is this line doing ?

PWMvalue[bin];

How can you be sure that 'bin' is actually a valid index in the array ? Your array has 5 elements, and 'bin' can be larger.

It is not allowed to put 13 numbers into an array of 11 elements.

OK got my errors cleaned up after a little bit of study and it appears to be working now and ran it from 0-100 hz input and hitting all of the array values in sequence on increasing and decreasing input frequencies.
I am not quite understanding the int bin = (freq+5) / 10 purpose running in the Loop?

Modified_Frequency_counter_sketch.ino (1.41 KB)

This

int bin = (freq + 5) / 10;

calculates a variable 'bin' that is used as index in the array to get the PWM value.
It might work a little, but that's now how to code.

This might help:

void loop()
{
  float freq = readFrequency(1000);

  int bin = (freq + 5) / 10;
  if( bin >= 13)   // index outside the pwm array ?
  {
    Serial.println( "frequency out of range");
  }
  else
  {
    analogWrite(motorPin, PWMvalue[bin]);

    Serial.println(freq);
    Serial.println(PWMvalue[bin]);
  }
  delay(1000);
}

The '13' is the number of elements of the array, you could replace that with sizeof:

int n = sizeof( PWMvalue) / sizeof( int);   // n is number of elements of array

I will plug your code suggestions in and try it .

Thank you very much

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.