Hello
Know any a way to do a scketch in a schetch?
Example: when scketch1 on the arduino detect a button interupt then open schetch2 on the arduino.
Can any give a example or tell how to do this please?
Thanks
Hello
Know any a way to do a scketch in a schetch?
Example: when scketch1 on the arduino detect a button interupt then open schetch2 on the arduino.
Can any give a example or tell how to do this please?
Thanks
Know any a way to do a scketch in a schetch?
No, but you can have a separate function that runs when a button press is detected.
start of loop
if button1 is pressed
call functionX
end of if
end of loop
functionX
do stuff here
either stay in the function
or return to where it was called
end of function
What is it that you are trying to do ?
I'm not sure i understood right, but maybe what you are looking is a State Machine.
State Machines work like this:
You create "States", let's say for example there are 3 States.
when your sketch starts the State is 1 and things work a certain way.
then when you press a button State becomes State 2 and it can work in a different way.
Then press again and State becomes State 3 and your sketch can still so something different.
The sketch would look something like this:
// all your variables and other needed stuff here
byte State = 0; // we will have 3 States; State 0, State 1 and State 2
// we start with State = 0
void setup(){
// all your setup code here
}
void loop(){
// check your button
// if the button was pressed
// increase State by 1
// if State is equal to 3, make it again equal to 0 - we are using only 3 States
// Check current State and run only the appropriate code
// if State is equal to 0 run just State0 code
if (State == 0) {
// Your State0 code here
}
// if State is equal to 1 run just State1 code
if (State == 1) {
// Your State1 code here
}
// if State is equal to 2 run just State2 code
if (State == 2) {
// Your State2 code here
}
}
State Machines are very useful and allow you to run specific pieces of code (you can kind of think of them as little sketches) according to the State the sketch is currently in.
I hope you got the idea of what i meant, if not just let us know and we can try to explain it better. If you look for State Machine in google you will find a lot of results which might also help you.
![]()
arduino123456789:
HelloKnow any a way to do a scketch in a schetch?
Example: when scketch1 on the arduino detect a button interupt then open schetch2 on the arduino.
Can any give a example or tell how to do this please?Thanks
As Boguz says, state machine will let you do it.
But not everything will be controlled by the general process-state. The button must be free to detect quickly at any time and debounced as well unless the button is hardware-debounced.
You don't need or want an interrupt unless that's one very special no-bounce button that HAS to detect a change in button state (ON/OFF) quicker than about 10 millis. This would include light-beam interrupt, magnetic sensing, and in general non-physical-contact-mashing sensing methods. If by button you mean actual finger-press buttons or you'd like to learn a bit about those then keep reading.
Button/contact bounce:
Most buttons, especially cheap buttons (I use jumpers for real cheap buttons just to test ideas) do not make solid contact at once but can take many millis of back and forth between ON and OFF reads done at high speed.
Fast code, the kind you want, will see several to 20+ button presses in a single bounce.
Arduino when not tied down by blocking code will look at that button on the 10's of microsecond level or faster.
What to do is have code that only watches the button and waits for for the button state to settle down, to stay ON or OFF for a certain amount of time like 5 millis.
OTOH you can use delay-code and assume it will be right. It will take longer than needed to ensure always being "right" and keep anything else from running until that wastey wait is done, UGH.
Neither way needs an interrupt. The action of buttons is far slower with far more slop than that. But no worries about that since the hand pressing the button and the mind behind it are 100 times slower and can't possibly notice the bit of time spent debouncing. To the human, it's all instant. For most wants, that is just great. You don't need an interrupt to achieve that, just nothing in the rest of your code that hangs button checking up!
You do not want delay() or long sequences of operations stopping anything like button/sensor/timing code, those are hang-ups.
Yump, all that just for a button, real-time code just to make sure it's done being pressed or released! And once it has the button code changes a variable that triggers the sketch2 code. Sketch2 does not trigger on the button pin but rather the abstracted, cleaned-up, for-sure-the-button-is-staying-ON-or-OFF state flag to act upon.
And I hope you have no doubt about it given all the why and how above.
May that be the hardest part of your project, but Murphy tells me it's very unlikely.
arduino123456789:
Can any give a example or tell how to do this please?
The demo I wrote in the first post in this Thread shows how a program can be built up from several small functions. One of them is triggered by a switch. The demo also illustrates the use of the technique in the Blink Without Delay example sketch to time various things with using the delay() function.
...R
Know any a way to do a scketch in a schetch?
Example: when scketch1 on the arduino detect a button interupt then open schetch2 on the arduino.
Can any give a example or tell how to do this please?
That's an unusual question . Why are you asking that ?
Normally, when people post here , they are asking how to do something, not how to call a sketch from another sketch.
When you think about it , the question doesn't make any sense because a sketch is just a program with code. Why would you put the code you want to run in a separate sketch ? Does that make any sense ? If you want to call a function in a sketch, why wouldn't
you put that function in the sketch from which you want to call it ?
Can you explain why you are asking such a strange question ?
The OP just wants to combine 2 sketches so that action in the first sketch code causes the incorporated second sketch code to execute.
The OP just wants to combine 2 sketches so that action in the first sketch code causes the incorporated second sketch code to execute.
It is not uncommon to get posts where people are asking how to combine the code from two sketches but it is unusual to get requests for how to CALL one sketch from another.
I don't think that the OP is that code literate.
The statements don't compile. Good thing I'm not a compiler, or at least not literally or even literal.
And anyway, the OP should find out what's possible soon enough.
Since he hasn't posted the code this thread is purely academic at this point.