220 likes | 309 Views
Mid Term Review. Voltage. Voltage is a measure of the electrical energy of a circuit. It is measured in Volts . The quality of energy. Current. Current is a measure of the magnitude of the flow of electrons in a circuit. It is measured in Amperes, or Amps . How much energy is Available.
E N D
Voltage • Voltage is a measure of the electrical energy of a circuit. It is measured in Volts. The quality of energy
Current • Current is a measure of the magnitude of the flow of electrons in a circuit. It is measured in Amperes, or Amps. How much energy is Available.
Resistors • The resistor can be defined by it's main purpose, a device to control or limit the flow of current, hence we can say that the main parameter of a resistor is it's resistance, which is measured in Ohm's . • they can also be used as voltage dividers to generate very precise voltages out of bigger voltages.
OHM’s Law • V = voltage • I = current • R = resistance • Ohm’s law shows how V, I and R are related
Parallel vs. series • parallel • series
Program Structure • Two required functions in an Arduino sketch, setup() and loop(). • setup() The function is called when your program starts. Use it to initialize your variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board. • Loop() After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
Variables • Let you store a value. • Must declare variables and tell computer what kind of value to expect. 3 types we use most: Byte, Character, Integer
Binary Numbering (how computers count) • If computers only know 1 and 0 how do they form complex operations? • 128 64 32 16 8 4 2 1 • 00000001 = 1 • 00000010 = 2 • 00000011= 3 • 00000100 = 4
digitalRead(pin) • Reads the value from a specified pin, it will be either HIGH or LOW. • pin: the number of the digital pin you want to read. • Returns either HIGH or LOW
Pull down resistor • you need the connection to ground as a reference point in digital inputs. Ensures the microcontroller “sees” ) volts when not receiving an input voltage.
If statement • A conditional which is used in conjunction with a comparison operator, tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is: if (someVariable > 50) { // do something here }
If…..else if (pinInput < 500) { // action A } else { // action B }
Inverted logic • For the data transmission above, a high voltage indicates a bit value of 1, and a low voltage indicates a voltage of 0. This is known as true logic. • Many serial protocols use inverted logic, meaning that a nigh voltage indicates a logic 0, and a low voltage indicates a logic 1. It’s important to know whether your protocol is true or inverted.
synchronous serial communication: both devices share a clock. • asynchronous serial communication: instead, both devices have their own clock and agree on a rate to which to set their clocks. Easier to implement.
PWM In the graph below, we pulse our pin high for the same length of time we pulse it low. The time the pin is high (called the pulsewidth) is about half the total time it takes to go from low to high to low again. This ratio is called the duty cycle. The duty cycle is 50%,and the average voltage is about half the total voltage.
If we make the duty cycle less than 50% by pulsing for a shorter amount of time than we pause, we get a lower effective voltage: The pulsewidth is usually a very small time, on the order of a few microseconds or milliseconds at most.
Mini review:Functions to know • pinMode(pin, mode) • digitalWrite(pin, value) • digitalRead(pin) • analogRead(pin)
Reading Code • Given the sample code below what would I change if I wanted to make pin 12 go ON for half a second and OFF for 2 seconds? • int ledPin = 13; • void setup() • { • pinMode(ledPin, OUTPUT); • } • void loop() • { • digitalWrite(ledPin, HIGH); • delay(1000); • digitalWrite(ledPin, LOW); • delay(1000); • }
What does this code do? • int ledPin = 13; • int inPin = 2; • int val = 0; • void setup() { • pinMode(ledPin, OUTPUT); • pinMode(inPin, INPUT); • } • void loop(){ • val = digitalRead(inPin); • if (val == HIGH) { • digitalWrite(ledPin, HIGH); • } else { • digitalWrite(ledPin, LOW); • } • }
What does this code do? • int potVar = 2; • void setup() { • Serial.begin(9600); • pinMode(11, OUTPUT); • } • void loop() { • potVar = analogRead(0); • Serial.println(potVar, DEC); • delay(10); • }