programming problem

Hello, I am a beginner in programming and I have the following problem. I need one minute timers for upper and lower limit sensors and a counter for the upper limit sensor, but I don’t know what to do. Could someone help me?

Here’s the code:

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

// Define our six input button pins
#define LEFT_PIN 4
#define STOP_PIN 3
#define RIGHT_PIN 2
#define START_PIN 5
#define UPPERLIMIT_PIN 7
#define LOWERLIMIT_PIN 6

// Define our analog pot input pin
#define SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 500
#define MIN_SPEED 0.1

void setup() {
// The only AccelStepper value we have to set here is the max speed, which is higher than we’ll ever go
stepper1.setMaxSpeed(10000.0);

// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode(START_PIN, INPUT_PULLUP);
pinMode(UPPERLIMIT_PIN, INPUT_PULLUP);
pinMode(LOWERLIMIT_PIN, INPUT_PULLUP);
}

void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.

// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(START_PIN) == 0) {
sign = -1;
}
else if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
else if (digitalRead(UPPERLIMIT_PIN) == 1) {
sign = 0;

}
else if (digitalRead(LOWERLIMIT_PIN) == 1) {
sign = 1;

}
// We only want to read the pot every so often (because it takes a long time we don’t
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter–;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot’s value from min to max speeds
current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}

// This will run the stepper at a constant speed
stepper1.runSpeed();
}

I need one minute timers for upper and lower limit sensors and a counter for the upper limit sensor, but I don’t know what to do.

Start by explaining what you mean by “one minute timers” and “a counter”.

Please remember to use code tags when posting code

the program controls the sled up and down. When the tray is on the sensor, the timer keeps it down for a set time and then rises up. in the top position same. The counter calculates how many times the motion is repeated. For example, after five repetitions, the sled stops up and stays there. the restart will be restarted from the start button.

Sorry i dont understand, what do you mean, Please remember to use code tags when posting code?

hatefinland:
, what do you mean, Please remember to use code tags when posting code?

I mean, when you post code, remember to put it inside code tags.

Sorry - I don’t know how to state it more simply.

can you put an example so i know what to do?

hatefinland:
can you put an example so i know what to do?

There is a thread at the top of every forum called "How to use this forum - please read". There are detailed instructions in there.

Steve

Hello, I am a beginner in programming and I have the following problem. I need one timers for upper and lower limit sensors and a counter for the upper limit sensor, but I don’t know what to do. Could someone help me, please. :confused:

the program controls the sled up and down. When the tray is on the sensor, the timer keeps it down for a set time and then rises up. in the top position same. The counter calculates how many times the motion is repeated. For example, after five repetitions, the sled stops up and stays there. the restart will be restarted from the start button.

Here’s the code:

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

// Define our six input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
#define  START_PIN 5    
#define  UPPERLIMIT_PIN 7  
#define  LOWERLIMIT_PIN 6  

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speed, which is higher than we'll ever go 
  stepper1.setMaxSpeed(10000.0);
  
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  pinMode(START_PIN, INPUT_PULLUP); 
  pinMode(UPPERLIMIT_PIN, INPUT_PULLUP); 
  pinMode(LOWERLIMIT_PIN, INPUT_PULLUP); 
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
  
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(START_PIN) == 0) {
    sign = -1;
  }  
  else if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {    
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }
else if (digitalRead(UPPERLIMIT_PIN) == 1) {
    sign = 0;
    
  }
  else if (digitalRead(LOWERLIMIT_PIN) == 1) {
    sign = 1;
    
  }
  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}

It sounds like your program is always in one of several states

STOPPED_AT_TOP_AWAITING_START_SIGNAL
MOVING_UP
PAUSED_AT_TOP
MOVING_DOWN
PAUSED_AT_BOTTOM

If so, then a state machine would seem to be the answer.

Execute the commands for the current state, then when it ends due to the position of the sled, time passing or the counter reaching its limit, move to the next state and execute its commands

Declaring the states as an enum and using switch/case based on the current state makes writing this relatively easy and easy to read and understand

Thanks for the answer. Can you write you or someone else to write a code for me because I don’t understand much about coding. Logic programming is familiar to me but not c-language… :o

Harri

Can you write you or someone else to write a code for me

Well, I could, but this section of the forum is really to help with problems encountered with code that has been written or adapted by the original poster.

The Gigs and Collaborations section of the forum is where you should ask for someone to write the code for you but you may have to pay them.

If you want the thread moved there then click the Report to moderator link in your original post and ask for it to be moved.

Now located in "Gigs".

Thread locked.