Arduino 7 – Motors and Functions Mr. McBrien TEJ3M Yesterday Inputs Today Functions Movement Functions Functions are modular code that can be reused again and again. We can write our own functions, and call them as we need them. Functions can have parameters – custom inputs that enable situation-specific execution. Anatomy of a C Function The Function with no Return Void functions have no return. We call them by simply invoking (no callFunction=…) Functions with no Parameters? Functions don’t have to have parameters, either! We can trigger a function that simply does something… Activity 1 Modify your blink code. Create a function that blinks an LED on for 1 second, then turns it off. Implement a function call in the loop section. Your code should call the blink function and then wait 2 seconds, and call it again. Movement There are a huge number of applications that need motors Motors have certain attributes that make them different than other components… Motors are Special… Disobey Ohm’s Law - PWM High voltage High current Back EMF They have interesting applications with current running backwards through them… How do we run a Motor at 1/3 Power? PWM! So how do we cope with the other things? Ans: our first shield! Why a Motor Controller? The Arduino’s not good for handling lots of current. Motors draw a LOT of current, and need higher voltages. The Ardumoto can manage 2A per channel. There are other motor shields; they all work in roughly the same way. Back EMF A key concern with controlling motors. When you remove the potential from a running motor, the current briefly reverses. All circuits involving electronic control of motors should include a flyback diode: Reverse current is dissipated. You don’t burn out your power supply. The good news is that the Ardumoto includes integrated diodes – no need to worry about back EMF. Ardumoto Commands The folks at Sparkfun have written functions for us. We can call the functions with motor ID, direction, and power level. Braking Stopping the Ardumoto: You can just use analogWrite(pin#,0); as an alternative. Pulse Width Modulation This again? Instead of changing the voltage, we briefly drop the voltage to zero We wait a period of time and then bring it back up to the “on” state. Limitations of the Ardumoto 2A max output current 18 V maximum in The Motor Pins LED Indicators Want to test your sketch without using motors? The blue and yellow LEDs indicate the direction your motors are being told to turn. The LED indicators even work when no motors are connected! Downloading Code There is a complete sketch available from Sparkfun: https://learn.sparkfun.com/tutorials/ardumoto-shieldhookup-guide?_ga=1.51874991.704449110.1481158941 The Pins Key Points Functions are sets of code that can be called at any time in our Sketch. Motors have some important differences from other components. With motors, we have to worry about back EMF! The Ardumoto includes flyback diodes so that we don’t need to. Motors support forward/back. Speed is controlled through the use of PWM. Homework Complete the assignment for today. References https://www.sparkfun.com/products/13201 https://learn.sparkfun.com/tutorials/ardumoto -shield-hookupguide?_ga=1.51874991.704449110.1481158941 https://www.arduino.cc/en/Reference/Functio nDeclaration