If( timeNow - timeSwitch >= SWITCH_READ_INTERVAL ) Maybe something like this: bool SwitchRead( void ) There's no reason to read a human-actuated switch every 10uS. You can use millis() or micros() to time things.Īn example might be a switch read. but without double counting during the same tripįloat time_passed = ((end_time-start)/1000000.0) įloat rpm_val = (hall_count/time_passed)*60.0 ĭelay(1) // delay in between reads for stabilityĬan we see your whole code and on what Arduino platform are you running?Ī state machine architecture looks something like this: void setup() counting number of times the hall sensor is tripped tCurrentPosition(-8000) //This stops the motor from looping until i can get the brake command working. delay(500) //crude form of button debouncing - 1000ms = 1s While(stepper.currentPosition() != -8000) tCurrentPosition(0) //set the current position to 0 lastModeState = modeState // save the current state as the last state, for next time through the loop Serial.println("Mode State No Increment") if the current state is HIGH then the button went from off to on: If (buttonState = LOW & modeState != lastModeState) ModeState = digitalRead(modePin) // read the pushbutton input pin (high or low)and assign it to buttonState:īuttonState = digitalRead(buttonPin) // read the pushbutton input pin (high or low)and assign it to buttonState: How can I run the RPM code while the Stepper motor is turning at the same time? void loop() so the stepper only turns a step then pause, step then pause for all modes selected. I am guessing it turns the motor 1 step pauses while running the added RPM code then repeats. When i run the code and select a mode the motor turns very slow steps at a time. I added the RPM code inside the While loop of the stepper so it would record RPM while the motor is turning. It runs while steps do not equal an assigned amount of steps. I have code that will do this as intended but integrating this code to my stepper motor code has proven problematic. Now, I want to add a magnetic hall effect sensor to the mix and assign the RMP to a variable. Modes 1-4 run at a different speed based on which one is selected and everything works really well so far. There is a switch button that will start or stop the motor once mode is selected.Mode selection will set a variable for speed to the stepper motor.I have a momentary button that selects a mode 1 thru 4.I have written some code to control the speed of a stepper motor. I am new to programming so please bear with me.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |