How to create spreadsheet matrix in arduino for pin selection?

I'm not sure if matrix is the right way to describe this, but I was wondering if I can get some guidance on how to set up a file in Arduino that mimics this table: Google Spreadsheet Link

To give some background, I am making an SMT auto feeder. I plan to have 24 feeders on my machine. The ATtiny chip feeder motherboard has two PWM drivers for the sprocket dc motor (columns C to E on google sheet) and it also has three multiplexers to read the encoder output (columns F to M on google sheet).

I would like it so that when I send the arduino a command, such as:
Feeder 3 START FORWARD 4 the arduino program would execute it like this:

  1. Find rows referencing Feeder 4.
  2. Start forward PWM pins.
  3. Until hitting 4 full cycles of encoder square pulses.

I already have the code to make this work for the most part with two motors and a button for testing, see below. I obviously have to change it to work with the PWM drivers and multiplexer to read encoder count. But I'm not able to figure out how to set up a config (.h file?) to copy over all the settings in the google spreadsheet so the command can search for the correct row of pins and execute the code. Can anyone give me CLEAR instructions on how to do that or a tutorial that is very similar in nature to this program?

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 5 from +5V
  - 10 kilohm resistor attached to pin 5 from ground
  - Motor attached to mosfet that is turned on by pin 4

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won't change:
const int buttonPin = 5;    // the pin that the pushbutton is attached to
const int mosfetPin = 4;       // the pin that the LED is attached to

// Variables will change:
int encoderCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
String command; // tell the motor to start by sending "start" command

// ---------- SETUP ----------

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(mosfetPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  //Set Mosfet off to begin with for safety
  digitalWrite(mosfetPin, LOW);
}

// ---------- VOID LOOP ----------

void loop() {
  
 if(Serial.available()){
          String command;
          command = Serial.readString();
        Serial.print("Received command: "); // print command received 
        Serial.println(command); // print string received
          command.trim();
          if(command=="start"){
            digitalWrite(4, HIGH); 
            Serial.println("Feeder turned on");
          }
          else if(command=="off"){
            digitalWrite(4, LOW);
            Serial.println("Feeder turned off");
          }
        else {
        Serial.println("Invalid command");
          }
      }
    
    // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
  
    // compare the buttonState to its previous state
      if (buttonState != lastButtonState) {
      // if the state has changed, increment the counter
      if (buttonState == HIGH) {
        // if the current state is HIGH then the button went from off to on:
        encoderCounter++;
        Serial.println("Encoder pad detected");
        Serial.print("Detection number: ");
        Serial.println(encoderCounter);
      } else {
        // if the current state is LOW then the button went from on to off:
        Serial.println("Encoder pad disengaged");
      }
      // Delay a little bit to avoid bouncing
      delay(50);
      }
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;
  
  
    // turns on the LED every four button pushes by checking the modulo of the
    // button push counter. the modulo function gives you the remainder of the
    // division of two numbers:
        if (encoderCounter >4) {
        digitalWrite(mosfetPin, LOW);
        }
}

Click on "file" then "download" then "comma separated data" and download your spreadsheet data. Then use serial to send the data to the Arduino.

Paul

Thanks, however, I feel like I'm still missing some info. Do I then upload that data as a separate file and link it in the header of my main code? How do you code the arduino to reference a row in the table and activate the selected pins in that row?

shai:
Thanks, however, I feel like I'm still missing some info. Do I then upload that data as a separate file and link it in the header of my main code? How do you code the arduino to reference a row in the table and activate the selected pins in that row?

I think you need to write a program for your PC to create a header file, a ".h" file and create an array of that data with a name and then you can reference that name in your code so the compiler can find it.

Paul

Sorry, but this sounds way too complicated for what I want to do. Isn't it possible to do something like this? Read csv and assign variables - Storage - Arduino Forum

I don't want to insert an SD card. I want to compile and upload the google spreadsheet to the chip. This is a read-only spreadsheet. Example: Feeder 1 is selected, use PWM1, PWM2, Encoder Pin 1. When feeder 2 is selected, use PWM3, PWM4, Encoder Pin 2, etc..

I can use if or case statements such as IF FeederInput == Feeder1{ set all variables to this and that}, but this sounds like a messy and inefficient way to write code.

shai:
Sorry, but this sounds way too complicated for what I want to do. Isn't it possible to do something like this? Read csv and assign variables - Storage - Arduino Forum

I don't want to insert an SD card. I want to compile and upload the google spreadsheet to the chip. This is a read-only spreadsheet. Example: Feeder 1 is selected, use PWM1, PWM2, Encoder Pin 1. When feeder 2 is selected, use PWM3, PWM4, Encoder Pin 2, etc..

I can use if or case statements such as IF FeederInput == Feeder1{ set all variables to this and that}, but this sounds like a messy and inefficient way to write code.

It is certainly less messy than you custom modifying the Arduino IDE.

Paul

What about doing it something like this?

void setup() {
  Serial.begin(9600);
  int PWMaPin;
  int PWMbPin;
  int PWMAddress;
  int MuxENPin;
  int MuxEncoderInputPin;
  int S2 = 32;
  int S1 = 26;
  int S0 = 25;
  pinMode(S2, OUTPUT);  //MUX S2
  pinMode(S1, OUTPUT);  //MUX S1
  pinMode(S0, OUTPUT);  //MUX S0
}

void loop() {

  switch (var) {
    case Feeder1:
      #define PWMaPin 31;
      #define PWMbPin 30;
      #define PWMAddress 05;
      #define MuxENPin 23;
      #define MuxEncoderInputPin 1;
      digitalWrite(S2,LOW )
      digitalWrite(S1, HIGH)
      digitalWrite(S0, HIGH)

      break;
    case Feeder2:
      // statements
      break;
    default:
      // statements
      break;
  }

  //SOME CODE HERE TO EXECUTE THE MOTOR TO MOVE
}

#define is a compile time definition so that won't work

Paul_KD7HB:
I think you need to write a program for your PC to create a header file, a ".h" file and create an array of that data with a name and then you can reference that name in your code so the compiler can find it.

I won't pretend to understand what the OP is trying to do but your idea of writing a PC program to create .h file seems to me a convenient way to make a large dataset available to an Arduino program.

A few lines of Python code should be all that is needed if the data is available to the Python program as a CSV file.

...R