0

I have this code, but its not working. If I type "ReportFRQ 1" (or 0) it reports back that FrqON variable has changed but the behaviour of the loop doesnt. Basically I am trying to switch parts of the loop on and off using variables, which are updated by serial command.

The main body of the code was kindly provided by another answer as a way of commanding the arduino remotely with custom commands. I can get the arduino to do various things, but updating the behaviour of the loop by activating and deactivating different variables does not seem to work...

Any ideas? Thanks

#include <avr/pgmspace.h>
#include <FreqCounter.h>

#define BUF_LENGTH 128  /* Buffer for the incoming command. */

unsigned long int frq;
unsigned int frqON;

static bool do_echo = true;

/* Execute a complete command. */
static void exec(char *cmdline)
{
char *command = strsep(&cmdline, " ");

if (strcmp_P(command, PSTR("help")) == 0) {
    Serial.println(F(
        "echo <value>: set echo off (0) or on (1)\r\n"
        "freq: reports current frequency"
        ));
} else if (strcmp_P(command, PSTR("echo")) == 0) {
    do_echo = atoi(cmdline);
} else if (strcmp_P(command, PSTR("freq")) == 0) {
       FreqCounter::f_comp= 8 ;             // Set compensation to 12
       FreqCounter::start(10);            // Start counting with gatetime of 100ms
       while (FreqCounter::f_ready == 0)         // wait until counter ready
       frq=FreqCounter::f_freq;            // read result
       if (frq<1) Serial.println("OFF");
       if (frq>1) Serial.println(frq); 
} else if (strcmp_P(command, PSTR("ReportFRQ")) == 0) {
       int frqON = atoi(strsep(&cmdline, " "));
       Serial.println(frqON);

} else {
    Serial.print(F("Error: Unknown command: "));
    Serial.println(command);
 }
 }

 void setup()
 {
Serial.begin(57600);
Serial.println("Frequency Counter");
}

void loop() {

/* Process incoming commands. */
while (Serial.available()) {
    static char buffer[BUF_LENGTH];
    static int length = 0;

    int data = Serial.read();
    if (do_echo) Serial.write(data);
    if ((data == '\b' || data == '\177') && length) {  // BS and DEL
        length--;
        if (data == '\177') Serial.write('\b');
        Serial.write(" \b");
    }
    else if (data == '\r') {
        if (do_echo) Serial.write('\n');    // output CRLF
        buffer[length] = '\0';
        if (length) exec(buffer);
        length = 0;
    }
    else if (length < BUF_LENGTH - 1)
        buffer[length++] = data;
 }

 if (frqON = 1) {
        frq=FreqCounter::f_freq;            // read result
       if (frq<1) Serial.println("OFF");
       if (frq>1) Serial.println(frq); 
 }

    /* Whatever else needs to be done... */
 }
  • 1
    while (FreqCounter::f_ready == 0) If that is supposed to be a loop with no body you need while (FreqCounter::f_ready == 0); Or, to make it even more clear: while (FreqCounter::f_ready == 0) /* noop */; Either way, you need the semi-colon or empty braces: while (FreqCounter::f_ready == 0) {} – Johnny Mopp Jun 28 '17 at 19:05
  • Sorry, can you please elaborate what effect that would have upon my code? If it can make it better obviously I want to but I would like to learn more - as this seems to work with the changes in the answer below... Thanks in advance – Rendeverance Jun 28 '17 at 19:10
  • The next line frq=FreqCounter::f_freq; will get executed each iteration of the while loop - it will be the body of the while loop. In this case, there may be no side-effects, but that may not always be the case. Also, if the condition FreqCounter::f_ready returns true on the first iteration, that line will never be executed. – Johnny Mopp Jun 28 '17 at 19:23
0

You have multiple problems here. First one:

You have a global variable:

unsigned int frqON;

However in your exec function you're creating a variable of the same name and writing to it. This doesn't change the global variable.

else if (strcmp_P(command, PSTR("ReportFRQ")) == 0) {
       //creates new local variable "frqOn" which overshadows global variable
       int frqON = atoi(strsep(&cmdline, " "));
       Serial.println(frqON);    
} 

To solve this and change the global variable, simply omit the int decleration.

Also your if check is wrong:

if (frqON = 1) {
        frq=FreqCounter::f_freq;            // read result
       if (frq<1) Serial.println("OFF");
       if (frq>1) Serial.println(frq); 
 }

When you do if(frqOn = 1) you set the variable frqOn to 1. You however want to compare the values, i.e. use the comparison operator ==.

  • Many thanks for looking at my code - all fixed now thank you greatly! I have taken onboard your tips and will apply them going forward (as I am only learning arduino, I am a hardware guy / engineer trying to branch out and digitise his projects) Thanks again :) – Rendeverance Jun 28 '17 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.