ATtiny 88 dev board, uploading via USB, latest Ardunio IDE.
I can only partially upload a sketch.
I can install sketches like examples etc but when I try to upload the sketch below it will only allow me upload and run 3 moves.
If I add another move is says it has uploaded but the ATtiny busy light stays blinking and fails to run.
I can pick any 3 moves but 3 is the max that works.
Any ideas pse?
Eddy
#include <Stepper.h> // Include the Arduino Stepper.h library
class Pause
{
public:
constexpr Pause(unsigned long milliseconds,
unsigned int seconds = 0,
unsigned int minutes = 0)
: m_milliseconds{milliseconds + (seconds * 1000) + (minutes * 60000)}
{};
constexpr unsigned long milliseconds() const {return m_milliseconds;};
private:
unsigned long const m_milliseconds;
};
struct Pin_Change
{
uint8_t const pin_number;
uint8_t const new_value;
};
enum Step_Direction {step_inwards = -1, step_outwards = 1};
struct Movement
{
size_t const number_of_pins_to_change_before;
Pin_Change const pin_changes_before[NUM_DIGITAL_PINS];
Step_Direction const step_direction;
size_t const steps_to_move;
long const speed_to_move_at;
uint8_t const travel_limit_input;
size_t const number_of_pins_to_change_after;
Pin_Change const pin_changes_after[NUM_DIGITAL_PINS];
Pause const pause_after_move;
};
void
setup()
{
/****************************
* Define the stepper motor *
***************************/
constexpr uint8_t motor_pins[] = {8, 10, 9, 11};
Stepper stepper_motor = Stepper{2048,
motor_pins[0],
motor_pins[1],
motor_pins[2],
motor_pins[3]};
constexpr long default_step_speed{5};
/*****************************
* Define the limit switches *
****************************/
constexpr uint8_t home_limit_switch{7};
constexpr uint8_t max_travel_limit_switch{6};
pinMode(home_limit_switch, INPUT_PULLUP);
pinMode(max_travel_limit_switch, INPUT_PULLUP);
/********************
* Configure ouputs *
*******************/
digitalWrite(13, LOW);
pinMode(13, OUTPUT);
digitalWrite(3, LOW);
pinMode(3, OUTPUT);
digitalWrite(4, LOW);
pinMode(4, OUTPUT);
digitalWrite(5, LOW);
pinMode(5, OUTPUT);
/********************************
* Define the movement sequence *
*******************************/
constexpr Movement moves[] =
{// Movement 1
{1, {{13, HIGH}}, // 1 pin change
step_outwards, 600, 15, max_travel_limit_switch, // Outward 600 steps, speed 15
0, {}, // No pin changes
{ 0, 2}}, // 2 sec pause
// Movement 2
{1, {{3, HIGH}}, // 1 pin change
step_outwards, 700, 3, max_travel_limit_switch, // Outward 700 steps, speed 3
0, {}, // No pin changes
{500, 2}}, // 2.5 sec pause
// Movement 3
{3, {{5, HIGH}, {4, HIGH}, {13,LOW}}, // 3 pin changes
step_inwards, 600, 10, home_limit_switch, // Inward 600 steps, speed 10
1, {{3, LOW}}, // 1 pin change
{0, 5}}, // 5 sec pause
// Movement 4
{1, {{13, HIGH}}, // 1 pin change
step_inwards, 0, 0, home_limit_switch, // No steps
0, {}, // No pin changes
{750}}, // 0.75 sec pause
// Movement 5
{0, {}, // No pin changes
step_inwards, 500, 10, home_limit_switch, // Inward 500 steps, speed 10
0, {}, // No pin changes
{250, 2}}, // 2.25 sec pause
// Movement 6
{2, {{13,LOW}, {3, HIGH}}, // 2 pin changes
step_inwards, 700, 10, home_limit_switch, // Inward 700 steps, speed 10
1, {{5, LOW}}, // pin change
{500, 2}}, // 2.5 sec pause
// Movement 7
{2, {{4, LOW}, {13, HIGH}}, // 2 pin changes
step_outwards, 500, 8, max_travel_limit_switch, // Outward 500 steps, speed 8
0, {}, // No pin changes
{750, 3}}, // 3.75 sec pause
// Movement 8
{3, {{5, HIGH}, {4, HIGH}, {13, LOW}}, // 3 pin changes
step_inwards, 800, 15, home_limit_switch, // Inward 800 steps, speed 15
3, {{5, LOW}, {4, LOW}, {3, LOW}}, // 3 pin changes
{ 0, 2}}}; // 2 sec pause
constexpr size_t number_of_moves{sizeof(moves)/sizeof(moves[0])};
/*****************************
* Run the movement sequence *
****************************/
stepper_motor.setSpeed(default_step_speed);
step_until_limit(stepper_motor, step_inwards, home_limit_switch);
make_moves(stepper_motor, moves, number_of_moves);
stepper_motor.setSpeed(default_step_speed);
step_until_limit(stepper_motor, step_inwards, home_limit_switch);
power_off_stepper(motor_pins);
}
void
loop()
{}
void
step_until_limit(Stepper& stepper_motor,
unsigned int const step_direction,
uint8_t const travel_limit_input)
{
while (HIGH == digitalRead(travel_limit_input))
{
stepper_motor.step(step_direction);
}
}
void
make_moves(Stepper& stepper_motor,
Movement const moves[],
size_t const number_of_moves)
{
for (size_t move_index = 0; number_of_moves > move_index; ++move_index)
{
make_move(stepper_motor, moves[move_index]);
}
}
void
make_move(Stepper& stepper_motor, Movement const & move_to_make)
{
change_pins(move_to_make.number_of_pins_to_change_before,
move_to_make.pin_changes_before);
move_steps_with_stop_limit(stepper_motor,
move_to_make.step_direction,
move_to_make.steps_to_move,
move_to_make.speed_to_move_at,
move_to_make.travel_limit_input);
change_pins(move_to_make.number_of_pins_to_change_after,
move_to_make.pin_changes_after);
delay(move_to_make.pause_after_move.milliseconds());
}
void
change_pins(size_t number_of_pins_to_change, Pin_Change const pin_changes[])
{
if (NUM_DIGITAL_PINS < number_of_pins_to_change)
{
number_of_pins_to_change = NUM_DIGITAL_PINS;
}
while(0 < number_of_pins_to_change)
{
--number_of_pins_to_change;
digitalWrite(pin_changes[number_of_pins_to_change].pin_number,
pin_changes[number_of_pins_to_change].new_value);
}
}
void
move_steps_with_stop_limit(Stepper& stepper_motor,
Step_Direction const step_direction,
size_t steps_to_move,
long const speed_to_move_at,
uint8_t const travel_limit_input)
{
if ((0 == step_direction) || (0 == steps_to_move) || (0 == speed_to_move_at))
{
return;
}
stepper_motor.setSpeed(speed_to_move_at);
while ((0 < steps_to_move) &&
(HIGH == digitalRead(travel_limit_input)))
{
stepper_motor.step(step_direction);
--steps_to_move;
}
}
void
power_off_stepper(uint8_t const motor_pins[4])
{
digitalWrite(motor_pins[0], LOW);
digitalWrite(motor_pins[1], LOW);
digitalWrite(motor_pins[2], LOW);
digitalWrite(motor_pins[3], LOW);
}