Can I associate a "string" with an array position?

I built an 8 head peristaltic dosing pump to be controlled with an Arduino over Ethernet through Blynk to my smartphone. The way the pumps work is I dial in a pump number and the amount of dosage in ml I want, then tick a button and the Arduino powers the respective pump on for a calculated amount of time and shuts off. Because of the way Blynk operates, I can forward all Serial.print commands in the form of terminal.print and the Blynk app displays that info on a monitor within the app on the smartphone.

All of that said, I am hoping there is an easy way to associate 8 different text strings with 8 different positions of the for loop. I have exhausted my ability to locate any similar projects on Google, plus I have exhausted my limited code skills when trying “different things” to make a scheme that will compile.

The below sketch runs perfectly if I can dial it back by deleting lines 21 and 22, and adjusting line 48 to simply print (x). What’s below is just my latest feeble attempt to make it compile. Research doesn’t suggest that I will find favorable “easy” results, but I am hoping someone with a fuller understanding can change that.

/*

*/
#include <SPI.h>                      //Used by Blynk
#include <Ethernet.h>                 //Used by Blynk
#include <BlynkSimpleEthernet.h>      //Used by Blynk
#include <SimpleTimer.h>
char auth[] = "PasteAuthKeyBetweenQuotes";       //Paste code that app emailed you between "quotes"

SimpleTimer timer;
WidgetTerminal terminal(V3);

int pumpPin[8] = { 2, 3, 4, 5, 6, 7, 8, 9 };         //Digital pins used
uint32_t multiplier[8] =  { 858, 827, 872, 865, 887, 895, 913, 843 }; //ms per ml
uint32_t startPump = 0;
uint32_t runCount;
float DOSEml;           //V1 Step Widget (0.25 per step, send step/NO, loop values ON)
int button = 0;         //V2 Button Widget set to Switch
bool pumpRunning = false;
int x;
const char y[8] = { "armorSi", "floraBlend", "caliMagic", "koolBloom",
                   "floraGro", "floraMicro", "floraBloom", "phDown" }

BLYNK_WRITE(V0) {                 // Choose Pump
  x = param.asInt() - 1; // Edited *****
}
BLYNK_WRITE(V1) {                 // Adjust Dosage
  DOSEml = param.asFloat();
}
BLYNK_WRITE(V2) {                 // Activate Chosen Pump
  button = param.asInt();
}

void checkPump()
{
  if (button == 1 && pumpRunning == false)
  {
    Blynk.virtualWrite(V0, 0);
    Blynk.virtualWrite(V1, 0);
    Blynk.virtualWrite(V2, 0);
    pumpRunning = true;
    digitalWrite(pumpPin[x], HIGH);
    startPump = millis();
    runCount = DOSEml * multiplier[x];
    terminal.print("Dosing in: ");
    terminal.print(DOSEml);
    terminal.print(" milliliters of ");
    terminal.println(y[x]);
    terminal.flush();
  }

  if (millis() - startPump > runCount)
  {
    digitalWrite(pumpPin[x], LOW);
    pumpRunning = false;
    button = 0;
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  timer.setInterval(1500L, checkPump);
  for (int p = 0; p <= 7; p++)
  {
    pinMode(pumpPin[p], OUTPUT);
  }
  Blynk.virtualWrite(V0, 0);
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);
}

void loop()
{
  Blynk.run();
  timer.run();
}

I’d like to emphasize that I just wish to associate the text string to the current position of [ x ] so the correct string prints with the correct pump number. Otherwise I can deal with the value display of the step widget (1-8) and mentally make the association.

Edit - Here is a YouTube video depicting the pump motors and the app and how they relate to one another.

myggle:
All of that said, I am hoping there is an easy way to associate 8 different text strings with 8 different positions of the for loop. I have exhausted my ability to locate any similar projects on Google, plus I have exhausted my limited code skills when trying “different things” to make a scheme that will compile.

have you looked into creating a struct?

I doubt your code compiles. You’re missing a semicolon after

const char y[8] = { "armorSi", "floraBlend", "caliMagic", "koolBloom",
                    "floraGro", "floraMicro", "floraBloom", "phDown"
                  }

and the variable y should be an array of pointers to arrays of characters.

I further strongly advise that you don’t use single character variable names as global variables. Give a meaningful name to x as well as to y.

To help with the question: you want to associate e.g. armorSi with the number 0 so you can send armorSi instead of 0?

If so, loop through the y array till you find the name

const char *y[8] = { "armorSi", "floraBlend", "caliMagic", "koolBloom",
                     "floraGro", "floraMicro", "floraBloom", "phDown"
                   };


void setup()
{
  Serial.begin(250000);
  int pumpnum = 0;
  for (pumpnum = 0; pumpnum < sizeof(y) / sizeof(y[0]); pumpnum++)
  {
    if (strcmp(y[pumpnum], "armorSi") == 0)
    {
      break;
    }
  }

  Serial.println(pumpnum);
  if (pumpnum >= sizeof(y) / sizeof(y[0]))
  {
    Serial.println("invalid");
  }
}

void loop()
{
 
}

BulldogLowell:
have you looked into creating a struct?

Only to the point of general knowledge of the most basics regarding structs. They extend functionality while holding different types at the cost of memory. Structs are still a topic I have yet to research, but because I want to use 8 strings coupled with the lack of knowledge has me wanting to continue to search for an easier option if at all possible.

sterretje:
To help with the question: you want to associate e.g. armorSi with the number 0 so you can send armorSi instead of 0?

I want the Arduino to recognize that position 0 is [ x ] and print armorSi. armorSi would then be displayed on the terminal monitor along with the rest of the sequnce of prints to pretty it up. Also, if say [ x ] were to have the value of 3, I would then want the Arduino to associate the string in the 4th position "koolBloom", and likewise for the other positions and respective strings.

Thank you for the other suggestions. I think I recognize strcmp as being part of Strings, but this is also something I have not yet researched. Though it seems that perhaps this is not a situation with a basic kind of solution and I may need to bite the bullet and begin reading and researching Strings and structs, that is if having these strings printed is important enough to me. Thanks again for the suggestions.

Is this what you need?

const char *y[8] = { "armorSi", "floraBlend", "caliMagic", "koolBloom",
                     "floraGro", "floraMicro", "floraBloom", "phDown"
                   };


void setup()
{
  Serial.begin(115200);
  Serial.println("Enter a digit character 0 - 7: ");
  
}

void loop()
{
   int index;

  if (Serial.available()) {
    index = Serial.read() - '0';  // Cheap way to make an int
    if (index >= 0 && index < 8) {
      Serial.print("The index entered was: ");
      Serial.print(index);
      Serial.print(" and the string is: ");
      Serial.println(y[index]);
    }
  }
}

It's not String (with capital S). strcmp() works on nul terminated character arrays, so called c-strings.

econjack:
Is this what you need?

const char *y[8] = { "armorSi", "floraBlend", "caliMagic", "koolBloom",

“floraGro”, “floraMicro”, “floraBloom”, “phDown”
                  };

Yes, this is exactly what I needed. The sketch now works as I hoped. Thank you all for leading me to the easy fix and I can yet again defer the need to learn structs and String class stuff into the future.

The way it works now is I dial in a pump#, dial in the respective dosage, and tick the button as demonstrated in the video link in my first post. The printed text reads something like the following;

Dosing in 2.75 milliliters of floraGrow. Thanks again!

/*

*/
#include <SPI.h>                      //Used by Blynk
#include <Ethernet.h>                 //Used by Blynk
#include <BlynkSimpleEthernet.h>      //Used by Blynk
#include <SimpleTimer.h>
char auth[] = "PasteAuthKeyBetweenQuotes";       //Paste code that app emailed you between "quotes"

SimpleTimer timer;
WidgetTerminal terminal(V3);

int pumpPin[8] = { 2, 3, 4, 5, 6, 7, 8, 9 };         //Digital pins used
uint32_t multiplier[8] =  { 858, 827, 872, 865, 887, 895, 913, 843 }; //ms per ml
uint32_t startPump = 0;
uint32_t runCount;
float DOSEml;           //V1 Step Widget (0.25 per step, send step/NO, loop values ON)
int button = 0;         //V2 Button Widget set to Switch
bool pumpRunning = false;
int x;
const char *y[8] = { "armorSi", "floraBlend", "caliMagic", "koolBloom",
                   "floraGro", "floraMicro", "floraBloom", "phDown" };

BLYNK_WRITE(V0) {                 // Choose Pump
  x = param.asInt() - 1;
}
BLYNK_WRITE(V1) {                 // Adjust Dosage
  DOSEml = param.asFloat();
}
BLYNK_WRITE(V2) {                 // Activate Chosen Pump
  button = param.asInt();
}

void checkPump()
{
  if (button == 1 && pumpRunning == false)
  {
    Blynk.virtualWrite(V0, 0);
    Blynk.virtualWrite(V1, 0);
    Blynk.virtualWrite(V2, 0);
    pumpRunning = true;
    digitalWrite(pumpPin[x], HIGH);
    startPump = millis();
    runCount = DOSEml * multiplier[x];
    terminal.print("Dosing in: ");
    terminal.print(DOSEml);
    terminal.print(" milliliters of ");
    terminal.println(y[x]);
    terminal.flush();
  }

  if (millis() - startPump > runCount)
  {
    digitalWrite(pumpPin[x], LOW);
    pumpRunning = false;
    button = 0;
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  timer.setInterval(1500L, checkPump);
  for (int p = 0; p <= 7; p++)
  {
    pinMode(pumpPin[p], OUTPUT);
  }
  Blynk.virtualWrite(V0, 0);
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);
}

void loop()
{
  Blynk.run();
  timer.run();
}