Arduino loop stops after for loop in called function

I am trying to make a radio controlled car which reacts to the frequency of music. The Loop works as it should and it gives me the correct frequency. The function berekenGrootte works too. This gives me the size of the frequency (max frequency - min frequency). But as soon as I want to use the function verdeel() which divides the size of the frequency in 10 to make the car move smoother it does it one or two times and then I don't get any feedback anymore in my serial monitor. Do I use too much Serial.print or do I need to restart the loop after the for loop in the second function?

#include <FreqMeasure.h>

void setup()
{
    Serial.begin(57600);
    pinMode(3, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(2, OUTPUT);
    pinMode(4, OUTPUT);
    FreqMeasure.begin();
}

double sum = 0;
int count = 0;
float lowest = 50;
float highest = 0;
float grootte = 50;
float b[9][1];

void loop()
{
    if (FreqMeasure.available()) {
        // average several reading together
        sum = sum + FreqMeasure.read();
        count = count + 1;
        if (count > 30) {
            float frequency = FreqMeasure.countToFrequency(sum / count);
            Serial.println(frequency);
            sum = 0;
            count = 0;
            if (frequency < lowest) {
                lowest = frequency;
                Serial.print("new lowest: ");
                Serial.println(lowest);
                berekenGrootte();
            }
            else if (frequency > highest) {
                highest = frequency;
                Serial.print("new highest: ");
                Serial.println(highest);
                berekenGrootte();
            }
            /*if(frequency > 1000){
                digitalWrite(3,HIGH);
                delay(1000);
                digitalWrite(3,LOW);
            }
            else{
                digitalWrite(5,HIGH);
                delay(1000);
                digitalWrite(5,LOW);
            }*/
        }
    }
}
void berekenGrootte()
{
    grootte = highest - lowest;
    Serial.print("new grootte: ");
    Serial.println(grootte);
    verdeel();
}
void verdeel()
{
    float plength = grootte / 10;
    b[0][0] = lowest;
    b[0][1] = lowest + plength;
    Serial.print("low: ");
    Serial.println(b[0][0]);
    Serial.print("high: ");
    Serial.println(b[0][1]);
    float startvalue = lowest + plength;
    for (int i = 1; i < 10; i++) {
        b[i][0] = startvalue;
        b[i][1] = startvalue + plength;
        startvalue = startvalue + plength;
        Serial.print(i);
        Serial.print(" low: ");
        Serial.println(b[i][0]);
        Serial.print(i);
        Serial.print(" high: ");
        Serial.println(b[i][1]);
    }
}

1 answer

  • answered 2018-01-12 04:56 Delta_G

    You're overstepping the bounds of your b array.

    float b[9][1];
    

    The array is only 9 * 1.

    void verdeel()
    {
        float plength = grootte / 10;
        b[0][0] = lowest;
        b[0][1] = lowest + plength;
    

    So there is no element b[0][1]. The second number can only go up to 0 since the array is size 1 in that dimension. An array of size 1 is pretty useless as an array.