Linux Setting Serial Port Baud Rate
Posted in HomeBy adminOn 10/10/17Arduino Lesson 6 Reading From the Serial Port. So far in our programming we have set the variables inside of the program, usually up at the top. In order to change the number of times the LEDs blink, we would change the lines of code that set those variables. This is OK for playing around, but you can see that if you want other people to use your programs you can not have them playing around with your code. You need to be able to get input from the user without modifying the code. Is the line that actually reads the data. Serial. parseInt reads the number the user input, and then that number is assigned to the variable numRedBlinks. We can do that using the Serial Port. Just like we can print information to the user using the Serial Port, we can also get information from the user using the serial port. In these exercises we will continue to use the circuit created in Arduino Lesson 3. If you need help in putting the circuit together, go back and review that lesson. Here is a diagram of the circuit we are working with. This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller. Also, as a reminder, here is the code we have been working with that incorporates all the things we have learned so far. Linux Setting Serial Port Baud Rate' title='Linux Setting Serial Port Baud Rate' />I am including this code so you can look at it if you get stuck, and to serve as an example for the work you do. To learn programming though, you need to be typing in your own code, making mistakes, and then finding an correcting your mistakes. You will not learn programming if you simply go through these lessons copying and pasting my code. LEDPin9 Declare red. Linux Setting Serial Port Baud Rate' title='Linux Setting Serial Port Baud Rate' />LEDPin an int, and set to pin 9. LEDPin1. 0 Declare yellow. LEDPin an int, and set to pin 1. On. Time2. 50 Declare red. On. Time an int, and set to 2. X-CTU1.png' alt='Linux Setting Serial Port Baud Rate' title='Linux Setting Serial Port Baud Rate' />Ah, Arduino, I remember when you were just crawling around and blinking LEDs. Now youre ready to learn how to speak In this lesson well learn how to use the Serial. Off. Time2. 50 Declare red. Off. Time an int, and set to 2. On. Time2. 50 Declare yellow. On. Time an int, and set to 2. Off. Time2. 50 Declare yellow. Off. Time an int, and set to 2. Yellow. Blinks5 Number of times to blink yellow LED. Red. Blinks5 Number of times to blink red LED. String red. MessageThe Red LED is Blinking Declaring a String Variable. String yellow. MessageThe Yellow LED is Blinking Declaring a String Variable. Serial. begin1. 15. Turn on the Serial Port. Modered. LEDPin, OUTPUT Tell Arduino that red. LEDPin is an output pin. Modeyellow. LEDPin, OUTPUT Tell Arduino that yellow. LEDPin is an output pin. Serial. printlnred. Message. for int j1 jlt num. Red. Blinks jj1 Start our for loop. Serial. print You are on Blink. Serial. printlnj. Writered. LEDPin,HIGH Turn red LED on. On. Time Leave on for red. On. Time. digital. Writered. LEDPin,LOW Turn red LED off. Off. Time Leave off for red. Off. Time. Serial. Serial. printlnyellow. Message. for int j1 jlt num. Yellow. Blinks jj1 Start our for loop. Serial. print You are on Blink. Serial. printlnj. Writeyellow. LEDPin,HIGH Turn yellow LED on. On. Time Leave on for yellow. On. Time. digital. Writeyellow. LEDPin,LOW Turn yellow LED off. Off. Time Leave off for yellow. Off. Time. Serial. LEDPin9 Declare red. LEDPin an int, and set to pin 9 intyellow. LEDPin1. 0 Declare yellow. LEDPin an int, and set to pin 1. On. Time2. 50 Declare red. On. Time an int, and set to 2. Off. Time2. 50 Declare red. Off. Time an int, and set to 2. On. Time2. 50 Declare yellow. On. Time an int, and set to 2. Off. Time2. 50 Declare yellow. Off. Time an int, and set to 2. Yellow. Blinks5 Number of times to blink yellow LEDintnum. Red. Blinks5 Number of times to blink red LEDStringred. MessageThe Red LED is Blinking Declaring a String Variable Stringyellow. MessageThe Yellow LED is Blinking Declaring a String Variablevoidsetup Serial. Turn on the Serial Port pin. Modered. LEDPin,OUTPUT Tell Arduino that red. LEDPin is an output pin pin. Modeyellow. LEDPin,OUTPUT Tell Arduino that yellow. LEDPin is an output pinvoidloopSerial. Message forintj1 jlt num. Red. Blinks jj1 Start our for loop Serial. You are on Blink Serial. Writered. LEDPin,HIGH Turn red LED on delayred. On. Time Leave on for red. On. Time digital. Writered. LEDPin,LOW Turn red LED off delayred. Off. Time Leave off for red. Off. TimeSerial. Serial. Message forintj1 jlt num. Yellow. Blinks jj1 Start our for loop Serial. You are on Blink Serial. Writeyellow. LEDPin,HIGH Turn yellow LED on delayyellow. On. Time Leave on for yellow. On. Time digital. Deuda Interna Y Externa Del Peru Pdf. Writeyellow. LEDPin,LOW Turn yellow LED off delayyellow. Off. Time Leave off for yellow. Off. TimeSerial. OK, look over this code and review what we have learned so far. At the top of the program we declare our variables, and we assign values to them. So far we have worked with variables of type int and type String. Then in the void loop we start our serial port, and we set our two arduino pins to OUTPUT. In the void loop we have built two for loops. LED and then one to blink the yellow LED. The parameters used in the for loop like how many times to blink and how long each blink should be are all defined at the top of the program. As we mentioned at the beginning, it is OK to start out doing things this way, but at some point you need to be getting your parameter values from the user, and not hard coding them into the program. You would like the program to ask the user how many times he would like to blink the red LED, and then ask how many times he would like to blink the yellow LED. This is really pretty easy to do, and we do it over the serial port, very similar to how we learned to print in lesson 5. In order to get input from the user, you need to make sure that you have turned your serial port on in your void setup. You do that with a Serial. You always need to have this command in your void setup if you are going to print to the serial port or read from it. Now, in order to get input from the user, you need to do three things Prompt the User for the Input. Wait for the User to Enter the Input. Read the information from the serial port. In the program above, lets say that in our void loop each time through the loop we want to prompt the user for how many times he wants the red LED to blink and then after that prompt him for how many times he wants the yellow LED to blink. In this scenario, we are now getting the parameters from the user instead of hard wiring them into the program. In this case, we still have to declare our varialbes, but we do not need to assign values to them. Hence in the example above the code. Yellow. Blinks5 Number of times to blink yellow LED. Red. Blinks5 Number of times to blink red LEDintnum. Yellow. Blinks5 Number of times to blink yellow LEDintnum. Red. Blinks5 Number of times to blink red LEDShould be taken out and replaced with. Yellow. Blinks Number of times to blink yellow LED. Red. Blinks Number of times to blink red LEDintnum. Yellow. Blinks Number of times to blink yellow LEDintnum. Red. Blinks Number of times to blink red LEDYou see, now we are only declaring our variables. We are not assigning values to them, because we will be getting the values from the user. It is important, however, that any variables that we will use still need to be declared. Now in our void loop this would be the code to get from the user the number of times he would like to blink the LED.