We will use a homing routine (see next step) to help keep everything in check. If you run the motor for a long time back and forth you may lose a count here and there and slowly lose track of where you are. Keep in mind that the counts may not be perfect. By knowing the current position and calculating the destination position we know which direction to send the motor. The subroutine speed0() will be linked to the interrupt. We are going to use a single variable and then depending if the actuator is going forwards or backwards we will add or subtract from the value. If you are counting pulses and the interrupt is too long you will lose counts.įor more details, see this page: Now that we've seen a basic interrupt we can expand it to control the motor. When you are in an interrupt you cannot be interrupted by a second one. Serial.prints() are a very computational intense task, they take a long time to happen. Usually it is poor form to use a Serial.print() in an interrupt. Serial.println(count) //see the counts advanceĭelay(100) //Delays usually can't be interfered with, here we will see the interrupt work volatile int count = 0 //if the interrupt will change this value, it must be volatileĭigitalWrite(2, HIGH) //enable internal pullup resistorĪttachInterrupt(digitalPinToInterrupt(2), interruptName, RISING) //Interrupt initialization Rising is whenever the pin sees a transition from low to high, falling is when it sees high to low, low is when the pin is low and high is when the pin is high.īelow is a small snippet of code, showing the basic interrupt we will use. By changing the trigger you can adjust when the interrupt happens. There are 4 types of triggers for the interrupt: Rising, Falling, High, and Low. This allows us not to lose track of counts. We are going to trigger an interrupt whenever the encoder gives pulses and use that to keep count. When you use interrupts, interrupt the code and execute as soon as the trigger condition is true. Usually when code runs it goes through line by line, running the instructions sequentially. They should only be a few lines of code and they need to execute as fast as possible. Interrupts are a type of subroutine, usually very small. We are going to be using the interrupt function of the Arduino. Using Ardunio pins 2 and 3 on the Uno is very important. In the next step we will look at the programming that will enable these pins as interrupts. If you have an Arduino Mega you have 6 interrupts that can be used (2, 3, 18, 19, 20, 21) and with a Due you can use every pin as an interrupt. The Arduino Uno has 2 interrupt pins that can be used. The Arduino pin selection of 2 or 3 is crucial. Wire two buttons between pins 7 and 8, connecting them to GND Once the motors are wired correctly to the boards, wire as follows: Ensure that "hall0" and "hall1" correspond to the correct MegaMotos (PWMA0 and PWMA1 respectively). Ensure that the jumpers on the MegaMoto and the hall effect sensors are all set to the correct pins. Make sure to check the beginning of the code in the next step. Actuator black wire to MOTB of the MegaMoto Actuator red wire to MOTA of the MegaMoto Yellow/Orange wire to Arduino pin 2 or 3 (Important) We are only using one of the hall effect signals. There are also the two actuator wires to connect to the MegaMoto. Each signal wire gives out pulses as the motor spins. The hall effect sensors have 4 wires: 5V, GND, and 2 Signal wires.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |