Corrosion due to absence from programming has hit me. A flooding of the celler sent all my books to the dumpster.
What is the syntax for a structure containing variables of the type int1, int2, int3?
Corrosion due to absence from programming has hit me. A flooding of the celler sent all my books to the dumpster.
What is the syntax for a structure containing variables of the type int1, int2, int3?
Thanks a lot!
I copied the example and this compiles well.
Code:
struct motor_struct
{
byte motor_I_O_pin;
byte motor_id;
byte motor_dir;
};
How do I create an array of 3 such structures?
This doesn't compile:
int motor_array[3] motor_struct
How do I acces the array?
Thanks. Managed to declare the struct. Next wish is to create an array of 3 such structss. It doesn't work.
Code is:
int motor_array[3] int motor_struct;
Error code is:
Combined_20190722a:69: error: expected initializer before 'int'
int motor_array[3] int motor_struct;
^
exit status 1
expected initializer before 'int'
motor_struct motor_array[3];
Accessing the array
motor_array[0].motor_I_O_pin;
motor_array[1].motor_I_O_pin;
motor_array[2].motor_I_O_pin;
When I checked, Mr. Google wasn't generating out-of-office replies. Any C++ site has all the details on declaring structs, arrays of structs, and how and why to use structs.
A struct to hold 3 ints is rather useless. An array would serve the same purpose.
A struct is useful when the types of data to relate are not all the same type.
@Romonaga Lots of thanks! That pulled plug!
A sample of the large code that compiles fault free:
struct motor_struct
{
byte sensor_pin;
byte motor_fwd;
byte motor_bwd;
};
motor_struct motor_stop_array[4];//Due to motors defined as 1,2,4
void setup()
{
bool status;
Serial.begin(115200);
motor_stop_array[motorX - 1].sensor_pin = x_limit_pin;
motor_stop_array[motorY - 1].sensor_pin = y_limit_pin;
motor_stop_array[motorZ - 1].sensor_pin = z_limit_pin;
reset_end_stop(motorX);
reset_end_stop(motorY);
reset_end_stop(motorZ);
// put your setup code here, to run once:
digitalWrite( 2, LOW);// digital x-step
digitalWrite( 3, LOW);
digitalWrite( 4, LOW);
digitalWrite( 5, HIGH);// digital x-dir
digitalWrite( 6, HIGH);
digitalWrite( 7, HIGH);
digitalWrite( 8, HIGH);//disable motorpower
digitalWrite(CNC_stop_ack, HIGH);//Take down acknowledge
pinMode(2, OUTPUT); //x-step
pinMode(3, OUTPUT); //y-step
pinMode(4, OUTPUT); //z-step
pinMode(5, OUTPUT); //x-dir
pinMode(6, OUTPUT); //y-dir
pinMode(7, OUTPUT); //z-dir
pinMode(8, OUTPUT); //z-dir
pinMode(13, OUTPUT);
pinMode(CNC_stop_req, INPUT_PULLUP);//A3
pinMode(CNC_stop_ack, OUTPUT);//D12
pinMode(x_limit_pin, INPUT_PULLUP);//D9
pinMode(y_limit_pin, INPUT_PULLUP);//D10
pinMode(z_limit_pin, INPUT_PULLUP);//D11
status = mylcd.begin(LCD_COLS, LCD_ROWS);
if (status) // non zero status means it was unsuccesful
{
status = -status; // convert negative status value to positive number
// begin() failed so blink error code using the onboard LED if possible
hd44780::fatalError(status); // does not return
}
mylcd.clear();
// Print start message to the LCD
mylcd.print("Started ");
delay(1000);
}
void reset_end_stop(int motor)
{
motor_stop_array[motor - 1].motor_fwd = false;
motor_stop_array[motor - 1].motor_bwd = false;
}
boolean no_stop (int motor, int dir)
{
if ( dir == fwd)
return (motor_stop_array[motor - 1].motor_fwd);
else
return (motor_stop_array[motor - 1].motor_bwd);
return (true);//This sould not happend
}
void motor_end_check (int motor, int dir)
{
bool tmp;
tmp = !digitalRead(motor_stop_array[motor - 1].sensor_pin);
if ( tmp)// Sensor is activated, set stop flag
if ( dir == fwd)
motor_stop_array[motor - 1].motor_fwd = tmp;
else//bwd
motor_stop_array[motor - 1].motor_bwd = tmp;
}
@PaulS
Of cource. Why wouldn't Google point out something? It just didn't hit me, search for C-language syntax there. On the other hand, I dislike clicking on unknown links in Google as well as several members dislike downloading
attached files in the topics.
Check the code extract. I'm home due to the help above.