Basic Programming
GETTING STARTED
Larissa, Andy, and I joined together to create a machine with multiple functions to learn a little about programming. The first step was to gather many sensors and motors and arrange them on a base plate. The next part was much more difficult. We logged onto a computer and followed some instructions telling us how to program our machine. Through the programming process we learned about output and input devices, digital and analog devices, and code sequences like loops.
input and output devices
Input and output function together. For example, if you were to type something into Google you would be 'inputting' information into the search bar. After you hit enter, you would get your search results, or 'output'.
Input devices are devices that will collect data in many forms. For example, on our machine we had an ultrasonic sensor which had the ability to sense the distance of things in front of it. It would collect this data and use it for an output device. The sensors on our machine were all input devices.
Output devices use data collected by input devices to function. For example, when you print a document you will need to use a mouse to command the printer to print (input) and the document that comes out of your printer is your output. On our machine, our sensors (input) collected data and told the motors when and in which direction to move (output).
Input devices are devices that will collect data in many forms. For example, on our machine we had an ultrasonic sensor which had the ability to sense the distance of things in front of it. It would collect this data and use it for an output device. The sensors on our machine were all input devices.
Output devices use data collected by input devices to function. For example, when you print a document you will need to use a mouse to command the printer to print (input) and the document that comes out of your printer is your output. On our machine, our sensors (input) collected data and told the motors when and in which direction to move (output).
analog and digital devices
The main difference between analog and digital devices is the digit systems that they have. Digital devices have a 2 digit system using only the digits 1 and 0. Analog devices use a different system that starts at zero and goes up to a much higher number than one depending on the device that you are using. For example, a button that you can push would be a digital device but an ultrasonic sensor would be an analog device because it measures how far away something is in numerical values greater than 1.
Code sequences
Analog, digital, input, and output devices all have one thing in common; they can only function if they have a code telling them what to do. The code is the boss of the entire operation. It's what tells the motors to go and in what direction, and what happens after a sensor senses something of value. There are many parts to a code. A code can include pseudo code, comments, and much more. A while loop is something that may be included in a code. In your code, this may look something like while(1==1). (1==1) means that the loop will run forever until you stop it.
If/else statements work together as a function. If you were working with a bump switch button and wanted to press the button to turn on a motor, you were would see something like:
{
if(SensorValue(bump1)==1)
{
startMotor(leftMotor, 60);
}
else
{
stopMotor(leftMotor);
}}
This code is saying that if you push the button, the sensor will be equal to 1 and the motor will start to rotate. If the button is pushed again, the sensor will no longer be equal to 1 and the motor will stop.
Open and closed loops are two more loops you will see in programming. An open loop contains constant variables and the system will perform the same way until a person or program makes a change. A closed loop is where variables collect data and makes corrections for a more efficient output.
If/else statements work together as a function. If you were working with a bump switch button and wanted to press the button to turn on a motor, you were would see something like:
{
if(SensorValue(bump1)==1)
{
startMotor(leftMotor, 60);
}
else
{
stopMotor(leftMotor);
}}
This code is saying that if you push the button, the sensor will be equal to 1 and the motor will start to rotate. If the button is pushed again, the sensor will no longer be equal to 1 and the motor will stop.
Open and closed loops are two more loops you will see in programming. An open loop contains constant variables and the system will perform the same way until a person or program makes a change. A closed loop is where variables collect data and makes corrections for a more efficient output.