Defining a variable

First off, I’d like to apologise if I use the incorrect nomenclature. Arduino’s code is my first programming language, so I’m not sure I’ve got all the terminology correct.

My code uses a library with some defined commands. Here’s an example:

#include <SoftwareSerial.h>// import the serial library
#include <legopowerfunctions.h> //Power Functions library

SoftwareSerial bluetooth(3, 2); // RX, TX
LEGOPowerFunctions lego(4); //IR pin for power functions control
int ledpin = 1; // led onboard will show blink on / off
int BluetoothData; // the data given from Computer
int FWDValue = 0; //variable for train forward speed

void setup() {
  // put your setup code here, to run once:
  bluetooth.begin(9600);
  pinMode(ledpin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (bluetooth.available()) {
    BluetoothData = bluetooth.read();
    if (BluetoothData == '1') { // if number 1 pressed ....
      digitalWrite(ledpin, 1);
      lego.SingleOutput(0, PWM_FWD1, RED, CH1);
    }
    if (BluetoothData == '0') { // if number 0 pressed ....
      digitalWrite(ledpin, 0);
      lego.SingleOutput(0, PWM_FLT, RED, CH1);
    }
  }
  delay(100);// prepare for next data ...
}

As you can see, if serial reads a value of 1 then the code runs a command containing PWM_FWD1. The library supports values PWM_FWDx from 1-7.

I understand that I could write seven “ifs” to cover each scenario (e.g. if serial == 5 then PWM_FLT5 and so on). I could also write a state machine to the same end.

But I wonder if there’s a more elegant way, where the x of PWM_FWDx could also be a variable, based on the value read from the serial input? That would make the code more lightweight, and avoid repetition.

Apologies if what I’ve written is garbage. I have the idea, but I’m struggling to put it into meaningful code.

Would a Switch Case help?

https://www.arduino.cc/en/Reference/SwitchCase

The library supports values PWM_FWDx from 1-7.

The library that you deliberately left out of your snippet, AND failed to post a link to?

Most likely, there is some relationship between the values of PWM_FWD1, PWM_FWD2, etc. that you could compute, so you needed only one if statement.

PaulS: The library that you deliberately left out of your snippet, AND failed to post a link to?

Sorry, I didn't know it was needed. I just wanted to keep the post simple, since I imagine it's a relatively simple topic for experienced coders.

Here's the pseudo-code I was thinking of:

int BluetoothData

void loop() {
   if (bluetooth.available()){
     BluetoothData=bluetooth.read();
   if(BluetoothData=='1'){   // if number 1 pressed ....
     digitalWrite(ledpin,1);
     lego.SingleOutput(0, PWM_FWD(BluetoothData), RED, CH1);
   }

I just don't know if that sort of thing is possible.

Grrrmachine: Sorry, I didn't know it was needed. I just wanted to keep the post simple, since I imagine it's a relatively simple topic for experienced coders.

We may be experienced, but we need to see what you are seeing. That's like going to an experienced doctor and not telling him all the symptoms you have because he is so experienced. :)

Ok, I've updated the code in the first post to provide more context. The library itself was emailed to me, but digging around it appears to be this one: https://github.com/schultzy51/LEGOPowerFunctions

How about using an array of values PWM_FWD1 to PWM_FWD7 and using the number from Serial to determine the index to the array.

      lego.SingleOutput(0, commandIndex[numberEntered], RED, CH1);
#define PWM_FWD1 0x1
#define PWM_FWD2 0x2
#define PWM_FWD3 0x3
#define PWM_FWD4 0x4
#define PWM_FWD5 0x5
#define PWM_FWD6 0x6
#define PWM_FWD7 0x7

Well, there certainly seems to be a relationship between the number in the name and the value associated with the name.