While the program is running, the motor will be moving, and you can watch the serial monitor to see what is happening. See the MegaMoto Instructable for more detail information about the motor controller. See the wiring diagram, and upload the code. This example is similar to the first example, we're just using time delays. We will use the MegaMoto to control an actuator to extend and retract. Now that we've tested different inputs, we can see how outputs can be used in the switch() statements as well. If (potValue <= 10) programCount = 3 //once the value is at the desired position, move on Serial.println("Middle position reached!") Serial.println(potValue) //show what the reading is While (potValue >= 500)//~512 is the middle Serial.println("Move the potentiometer to the middle") While (potValue = 1000) programCount = 1 //once the value is at the desired position, move on Serial.println("Move the potentiometer fully to one side to get the max reading") PotValue = analogRead(pot) //read the pot's value Serial.println("Move the potentiometer to the desired locations to advance") ProgramCount = 0 //start at the beginning of the program PinMode(pot, INPUT) //set the pot as an input Int potValue = 0 //variable to hold the pots reading The potentiometer must be moved to the desired valueīefore advancing to the next stage in the program.Ĭonst int pot = A0 //attach the pot to pin A0 * This code is to show how an analog input can be used to move through a Since these are just test programs, you can leave the board plugged into the computer so the Arduino can receive power. Wire the potentiometer according to the diagram, and upload the code below. The code will read the potentiometer, and only advance once the desired value is reached. Most sensors give some sort of analog signal, for this example we will be using a potentiometer. The next input we'll try is an analog input. Serial.println("Button has been held for 5 seconds") //print the number If (buttonCount = 5000) programCount = 1 //once the button is pressed enough times, move on Serial.println("Hold the button for 5 seconds to complete the program") īuttonCount = buttonCount+1 //count up every time the button is pressedĭelay(1) //small delay for counting milliseconds PinMode(button,INPUT) //set the button as an inputĭigitalWrite(button, HIGH) //enable internal pullup resistor Int buttonState = 1 //variable to hold the state of the button Int buttonCount = 0 //variable to hold the button presses The button must be pressed 5 times to moveĬonst int button = 5 //attach the button on pin 5 * This code is to show how a digital input can move through cases See the code below, then upload it to see how it works before moving to the next step. Watch the serial monitor to see when the program is done. Once it has been held for 5 seconds, we'll move to the next state. In the first case, we'll use a counter to store how long the button has been pressed. We'll use a button to trigger the movement to the next state. Now that we know the basics of a switch statement, we can add in a basic digital input. ProgramCount = 0 //start at the beginning Serial.begin(9600) // initialize serial communication: Int programCount = 0 //variable to move through the program To 10 in the serial monitor using a delay. * This code is to show the basic workings of a switch statement. See the attached code, read the comments, and upload it to see how it works before moving on. The switch statement sits in each case until 1 second has passed, then moves to the next. By watching the serial monitor, you can see the program count to 5 over 5 seconds. There is also a default case, so that you can put a section of the program to happen when the variable you are watching does not match one of the programmed cases.įor the first example, we'll have a passive code. This property makes it very easy to make a program pointer that will move through your code, applying all the appropriate inputs and outputs at the correct time. The basics of the switch() statement is that you watch a variable, and depending what the variable's value is, a different case in the switch() is activated. Before we dive into the coding, we need to understand what we are working with.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |