![]() As It's wrote below, the sketch is working perfectly, especially if considering the situation in which every function returns one single value. In particular I've Imagined a sketch with a 'switch case loop' for the function selection. Might have to tweak this a bit for proper pairings. Hi, I would like to create a sketch that execute some different function (sensor measurements) depending on which key is pressed by user. Next time thru loop(), see if a subcommand came in, if so save it and execute it. If so, save it, and clear out the prior subcommand so it doesn't execute it. So, I'm thinking you can have commands and nested subcommands.ĭo something like this - read the byte, determine if its a command I think the example I wrote above should cover what I'm trying to achieve. Yes, within a few milliseconds is fine as well. If you break your tasks into small pieces though, you can make it appear to the user that multiple things are happening at once. However, according to I should be checking if Serial.available first, before attempting to deal with 'p'.ĭon't forget that the Arduino is single threaded - it can only do one thing at a time. So Case A will run first, then whilst case A is running, user enters 'p' 10 times until position variable = 10. If Case A is running, and user would like to increase position to 10, then using your example, it would be like this. So basically, if I have the following: int position = 0 Yes I think this is more in line with what I want to accomplish. Presumably you will want other commands, 'b', 'c', 'd', that will do different things also, so you'd want I don't know about repeating 'p' while 'a' is running, but you could save the last thing received, and if the same one came in again you could use it over without needing and 'a'. Is there a way to have "p" code run each time I enter "p", whilst case 'a' is running? Why not put the breaks in if you are worried about them not being there, or alternatively set a variable. The breaks used in switch/case prevent execution of the code from falling through to the next case but your returns will do the same. However, the issue is that if the user enters "p" whilst case 'a' is running, the code works, however, if I enter "p" a few times one after the other, the code eventually stops running. Using return inside of switch case statement inside of function. Please choose another function to run simultaneously with me In this example, when the user enters "a" in the Serial Monitor input, the Serial Monitor will print move motor by X amount in X direction each time 'p' is entered in Serial Monitor Serial.print("This is first switch case.\nPlease choose another function to run simultaneously with me\n") Please note that the intention is to run both Case and the Code associated with Serial Monitor input. I was wondering if anyone has any idea how to receive input from Serial Monitor whilst a switch case is already running, and conduct an action based on that input. Hope everyone is having a great day today.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |