Arduino Forum

Using Arduino => Programming Questions => Topic started by: Railroader on Jul 20, 2019, 11:52 pm

Title: How to create, and read, an array indexed by "enums"
Post by: Railroader on Jul 20, 2019, 11:52 pm
Hi! I would like to use a 2 dimensional Array. The first index Points to 3 defined motors, motorX, motorY and motorZ. The second index is defined by the defined objects fwd resp. bwd.
How do I declare it and how do I read it?

The entire code is way to large to attach. I get a compiler error message at the last line, "bool motor_stop_array[motorZ, bwd];"

The array is supposed to carry stop flags for CNC axles running into an end of move switch.

Code: [Select]

//I2C for LCD
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
#define LCD_COLS 20
#define LCD_ROWS 4

#define x_channel A0
#define y_channel A1
#define z_channel A2

#define x_limit_pin 9
#define y_limit_pin 10
#define z_limit_pin 11

#define bwd HIGH
#define fwd LOW
#define motorX 1
#define motorY 2
#define motorZ 4


int prevxdir, prevydir, prevzdir;

/* 1 mm/sekund*/
//#define drill_diameter 30*steps_per_mm
#define X_steps_per_mm 400
#define Y_steps_per_mm 400
#define Z_steps_per_mm 400
#define backlashx 33
#define backlashy 16
#define backlashz 200
//400 steps per 1000 mS

#define stepdelay 1/*speed*1000/400*/
long xpos, ypos, zpos;
long xtmp, ytmp, ztmp;
long start_time = millis();
#define xtransport_delay 600//microo seconds delay per step
#define ytransport_delay 600//microo seconds delay per step
#define ztransport_delay 1000//microo seconds delay per step

#define xwork_delay 10000//microo seconds delay per step
#define ywork_delay 10000//microo seconds delay per step
#define zwork_delay 30000//microo seconds delay per step

#define safe_height 3200

#define CNC_stop_req A3 /* Feed Hold */
#define CNC_stop_ack 12 /* Spindle Enable */
int x_min = 1023, y_min = 1023, z_min = 1023;
int x_max = 0, y_max = 0, z_max = 0;
bool global_angle_print_flag = false;
bool global_depth_print_flag = false;
bool global_radious_print_flag = false;

bool motor_stop_array[motorZ, bwd];
Title: Re: How to create, and read, an array indexed by "enums"
Post by: UKHeliBob on Jul 21, 2019, 12:05 am
Each dimension in a multidimensional array needs the number of elements in its own square brackets and the data type must be the same for all elements of the array
Code: [Select]

bool anArray[2][3];  //an array of booleans with 2 rows each of 3 columns
Title: Re: How to create, and read, an array indexed by "enums"
Post by: david_2018 on Jul 21, 2019, 01:46 am
You are going to have problems using motorX/motorY/motorZ as an index when they are not defined as sequential numbers starting with 0.  The title is also a bit misleading, because you are not creating an enum, that would be something similar to this:

Code: [Select]

enum motors { motorX, motorY, motorZ };


Then you can create a variable of type motors, which can have values motorX, motorY, and motorZ, effectively equivalent to 0, 1, and 2.
Title: Re: How to create, and read, an array indexed by "enums"
Post by: noweare on Jul 21, 2019, 05:28 am
Quickly looking at your code maybe an array of struct would work

The struct would consist of motor, direction, limit sw  ..   etc. any attribute that each motor has then
create an array of them. In your case an array of 3.
Title: Re: How to create, and read, an array indexed by "enums"
Post by: Railroader on Jul 21, 2019, 06:48 pm
@UKHeliBob Thanks! I didn't check Arduino Reference when writing the question. Using probably different data types, int and who knows what will not work well then.

@david_2018 Thanks! My reason for defining the motors as 1, 2 and 4 gives me the possibility to command motor 3, 5, 6 and 7... In other words run 2 or 3 motors simultaniously. Then the array needs 4 of the first index and 2 of the second.

Yes, the title is a not at all good.

@noweare Thanks! I will probably try using structures.


Title: Re: How to create, and read, an array indexed by "enums"
Post by: UKHeliBob on Jul 21, 2019, 11:51 pm
Quote
I didn't check Arduino Reference when writing the question
The multidimensional array syntax is nothing to do with using an Arduino which uses C++ so use the C++ syntax or don't do it !
Title: Re: How to create, and read, an array indexed by "enums"
Post by: Railroader on Jul 22, 2019, 12:28 am
Okey. I was on a 2 day coarse in "C" in the year 1994 and they gave 1 hour, or 2, for C++ at the end of it. Then I used C for 14 years.
Anyway, reading the Reference solves a lot of questions.
And...., my sketch is running.
Title: Re: How to create, and read, an array indexed by "enums"
Post by: ee_es_pe on Jul 22, 2019, 03:15 am
Another option is to use a single array but use a multiplier to access to create the appearance of a 2d array. eg
Code: [Select]

const int size = 4;
const int motors = 3;
const int motor1 = 0;
const int motor2 = 1;
const int motor3 = 2;
bool motor_stop_array[motors * size]; // 4 item array. need initialization
bool state = motor_stop_array[motor1 * size + 2]; // access second item of motor 1

obviously this could be coded much better depending on situation but just presenting an idea.

..had to edit cos bugs
Title: Re: How to create, and read, an array indexed by "enums"
Post by: Railroader on Jul 22, 2019, 09:55 pm
As I wrote, I have solved the matter and the sketch is running using a more structured construction. Sometimes "djungle methods" are used when no other solution can be found but they often cause a lot of trouble.