Two programs in arfuino

Is it possible to store two programs in a single arduino and switch to one or the other with a toggle switch? Or alternately, is it possible to have distinct movements (this is a stepper program) in one program that can be selected via a toggle switch?

You cannot store 2 separate programs, but you can have a single program that does different things depending on an input

Oops, I meant ARDUINO! Not a good start :slight_smile:

what do you mean by two programs? a function is a program you can have 2 functions and select which one on demand from external input

Yes, that is what I meant. Here are the details of what is needed:

I already have one function that turns a stepper in one direction at a variable speed. What I need to do is have a second function that would turn the stepper by a constant number of steps in one direction and then reverse and turn the stepper the same # of steps, and repeat. This would be selectable by switch.

When you say "function" do you mean an Arduino program, or do you literally mean a function(). If the latter, have you incorporated it into a working Arduino program and got it working?

While you think about my previous question, yes, it is perfectly possible to do exactly what you ask for. The basic approach is to check the position of the switch, and then call the appropriate function. Presumably you will want to check the switch position continuously, which adds a degree of complication, but nothing very hard. The best approach would be for you to read up on how to read a switch position, and then have a go at producing some code which does that and takes the appropriate action.

If you have a go you'll get loads of help. Oh, and please show us these two programs/functions so we can see what you're working with.

One more question: these functions or programs that you've got - did you write them yourself or find them on the Internet? If the former, you probably could work out how to combine them into one program. If the latter, now would be a good time for you to start learning how to write code yourself.

One important point about switching between 2 functions with a switch is that either the switch position must be read in each function or the functions must be written such that they return to the loop() function frequently so that the switch state can be checked

In practice this means no delay()s in either function nor any long winded for loops or while loops. Instead you use millis() for timing

See Using millis() for timing. A beginners guide for examples

Cheers, Bob - I was going to come to that later, when we can see what the OP actually has. But yes, this is a great example of a project that needs a finite state machine for each "function" and rapid cycling round the loop() function.

@Widpyro has PM'd me asking if I would be willing to write the combined sketch for him. I have suggested posting them here so we know what we are dealing with. I expect to see plenty of delay()s and possibly for/while loops in one or other of them, but we will see

I will post the code I have when I get to my computer in a hour or so.
Yes, I could probably figure out how to write the code, and I look forward to spending some time with it. However, I just cannot do it right now and I have a convention coming up where I’m expected to demonstrate a new product. Bottom line, I not only need help but t a finished piece of software. I know this form doesn’t encourage this approach, but I’m in a bit of a pickle.
I’ll post the existing code and see what folks think. I am willing to pay and any interested parties can contact me at widpyro@charter.net

Topic moved to Jobs and Paid Consultancy category

1 Like

Sounds like this belongs in the section where bounties are paid, or whatever it's called here

Hello Stephen,

Here is the existing code I have. This moves the stepper motor in one direction at a variable speed.

#define potmeterPin A0

// defins pins numbers

#include <Stepper.h>;

const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;

int p;

float delay_time=50;

void setup() {

Serial.begin(9600);

// sets the two pins as Outputs

pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

pinMode(enPin,OUTPUT);
digitalWrite(enPin,HIGH);

// Set 500Dir to home switch
digitalWrite(dirPin,LOW); //enables te motor to move in a particular direction
}
void loop(){
p = analogRead(potmeterPin);

delay_time = map(p,0,1023,500,1000);

motorStep(1);

}

void motorStep( int MAX){

for(int x = 0; x < MAX; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(delay_time);
digitalWrite(stepPin,LOW);
delayMicroseconds(delay_time);
}

}

What I need to add to it is a second section of code that will:

  1. move the stepper in one direction for a constant # of steps. Next it would move the stepper in the other direction for the same # of steps. This would be accomplished by an on/off switch that would select which segment of the code is run

2)The speed of the stepper for the 2nd segment of code would be controlled by the same 10k pot that controls the speed of the first segment.

  1. Ideally, Id like the 2nd section of the code to start in the middle of the prescribed # of steps. That is, if the number of steps that the motor turns clockwise ( and counterclockwise) is 100, the code would start at step 50. It is not important where in a 360 degree revolution it starts, just that relative to step 0 and step 100, it starts in the middle (step 50).

That is it. Does that make sense to you? Have I adequately described it?

jim

Now is the time to learn how to post code

See How to get the best out of this forum

how much are you willing to pay to get this done?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.