List of previous values?

Hi, I have this code:

void setup(){
  Serial.begin(9600);
  Serial.println("Type 'h' for a commands list and syntax");
}

void loop()
{
   char c, t;
   int nnn;

   if(Serial.available() > 0) // If there is data to read
   {
       c = Serial.read(); // Get a character

       delay(50);

       if(c >= 'a' && c <= 'z') // If it's a command
       {
          nnn = 0;
          while(Serial.available() > 0) // While there is still data
          {
             t = Serial.read(); // Read next character

             if(t == ',' || t == ' ')
               continue; // Skip commas and spaces
             if(t >= '0' && t <= '9') // If it's a number
             {
                nnn *= 10; // Multiply previous value by 10
                nnn += t - '0'; // Add the new digit
             }
             else
                break;

             delay(50);
          }
       }
   }

   // Now, do something with c and nnn
   switch(c)
   {
      case 's':
        analogWrite(6,nnn);
          Serial.println("Rotation Fans @:");
          Serial.println(nnn);
        break;
      case 'f':
        analogWrite(3,nnn);
          Serial.println("Fog Volume Fan Speed @:");
          Serial.println(nnn);
        break;
        case 'u':
          analogWrite(5,nnn);
          Serial.println("Updraft Fan:");
          Serial.println(nnn);
          break;
        case 'r':
          analogWrite(9,nnn);
          Serial.println("Red Light @:");
          Serial.println(nnn);
          break;
        case 'g':
          analogWrite(10,nnn);
          Serial.println("Green Light @:");
          Serial.println(nnn);
          break;
        case 'b':
          analogWrite(11,nnn);
          Serial.println("Blue Light @:");
          Serial.println(nnn);
          break;
        case 'h':
          Serial.println("Commands: s = Rotation Fans | u = Updraft Fan | f = Fog Volume Fan | r = Red Light | g = Green Light | b = Blue Light");
          Serial.println("Syntax: command, value");
          Serial.println("Value = PWM Value between 0 and 255");
          break;
      // Add other letters here, with break after each one
   }
}

It works perfectly, though I thought it would be good if I could do something like “list”, and it would list each of the switch commands, and what value they are currently at?

Is this possible?

-Dan :slight_smile:

As you have it written, no, because you're doing an analogWrite with a temporary variable "nnn". If you had a variable that you associated with each device, copied "nnn" to that, then yes, you could do what you want.

Hi,

try this

void setup(){
  Serial.begin(9600);
  Serial.println("Type 'h' for a commands list and syntax");
}

void loop()
{
   char c, t;
   int nnn;
   int lastval[26]; // array for last values of command a..z
   bool dolist = false;

   if(Serial.available() > 0) // If there is data to read
   {
       c = Serial.read(); // Get a character

       delay(50);

       if(c >= 'a' && c <= 'z') // If it's a command
       {
          nnn = 0;
          while(Serial.available() > 0) // While there is still data
          {
             t = Serial.read(); // Read next character

             if(t == ',' || t == ' ')
               continue; // Skip commas and spaces
             if(t >= '0' && t <= '9') // If it's a number
             {
                nnn *= 10; // Multiply previous value by 10
                nnn += t - '0'; // Add the new digit
             }
             else
                break;

             delay(50);
          }
       }
   }

   // Now, do something with c and nnn
   do {
     switch(c)
     {
        // if list command, start list all commands from a..z if exist
        case 'l' : 
          // list command starts
          if (!dolist) {
            // start with command a
            c = 'a';
            // activate list command
            dolist = true;
          }
          break; 
      case 's':
          if (!dolist) {
            lastval[c-'a'] = nnn; // save the last value
            analogWrite(6,nnn); 
          }
          else {
            Serial.println("Rotation Fans @:");
          Serial.println(lastval[c-'a']);
          }
        break;
      case 'f':
          if (!dolist) {
            lastval[c-'a'] = nnn;
            analogWrite(3,nnn);
          }
          else {
          Serial.println("Fog Volume Fan Speed @:");
          Serial.println(lastval[c-'a']);
          }
        break;
        case 'u':
          if (!dolist) {
            lastval[c-'a'] = nnn;
          analogWrite(5,nnn);
          }
          else {
          Serial.println("Updraft Fan:");
          Serial.println(lastval[c-'a']);
          }
          break;
        case 'r':
            if (!dolist) {
              lastval[c-'a'] = nnn;
            analogWrite(9,nnn);
            }
            else {
            Serial.println("Red Light @:");
            Serial.println(lastval[c-'a']);
            }
          break;
        case 'g':
            if (!dolist) {
              lastval[c-'a'] = nnn;
            analogWrite(10,nnn);
            }
            else {
            Serial.println("Green Light @:");
            Serial.println(lastval[c-'a']);
            }
          break;
        case 'b':
            if (!dolist) {
              lastval[c-'a'] = nnn;
            analogWrite(11,nnn);
            }
            else {
            Serial.println("Blue Light @:");
            Serial.println(lastval[c-'a']);
            }
          break;
        case 'h':
            if (!dolist) {
              // list command sonly if list command not active
              Serial.println("Commands: s = Rotation Fans | u = Updraft Fan | f = Fog Volume Fan | r = Red Light | g = Green Light | b = Blue Light |  = List Values");
            Serial.println("Syntax: command, value");
            Serial.println("Value = PWM Value between 0 and 255");
            }
          break;
      // Add other letters here, with break after each one, a..z are allowed
     }
     // if list command active, next command character until z reached
     if (dolist && ++c >= 'z') dolist = false;
   } while (dolist);
}

Mike

Nice idea, but declaring the lastval array inside the loop function makes it a local variable that is reset every time loop starts. Moving the line outside of loop makes it a global variable, and persistant.