Go Down

Topic: How to create, and read, an array indexed by "enums" (Read 237 times) previous topic - next topic

Railroader

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];
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

UKHeliBob

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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

david_2018

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.

noweare

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.

Railroader

@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.


Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

UKHeliBob

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 !
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Railroader

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.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

ee_es_pe

#7
Jul 22, 2019, 03:15 am Last Edit: Jul 22, 2019, 03:21 am by ee_es_pe
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

Railroader

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.
Use Your knowledge. If that's not enough, look for education.
Having knowledge, think outside the box to gain more of it. Only trains run like the train, on the rails. The rest run between the rails.

Go Up