1 / 38

Arduino 1

Arduino 1. Basics. Comments

dominy
Télécharger la présentation

Arduino 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arduino 1

  2. Basics • Comments • /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We use pin 13 because, * depending on your Arduino board, it has either a built-in LED * or a built-in resistor so that you need only an LED. * * http://www.arduino.cc/en/Tutorial/Blink */ • // LED connected to digital pin 13

  3. Basics • In the Arduino environment programs are referred to as sketches • http://www.arduino.cc/ • http://arduino.cc/en/Reference/HomePage • http://arduino.cc/en/Tutorial/Foundations

  4. Basics • setup() and loop() • There are two special functions that are a part of every Arduino sketch: setup() and loop(). • The setup() is called once, when the sketch starts. It's a good place to do setup tasks like setting pin modes or initializing libraries. • The loop() function is called over and over and is heart of most sketches. • You need to include both functions in your sketch, even if you don't need them for anything.

  5. The pinMode() function configures a pin as either an input or an output. To use it: • You pass it the number of the pin to configure and the constant INPUT or OUTPUT. • When configured as an input, a pin can detect the state of a sensor like a pushbutton. • As an output, it can drive an actuator like an LED.

  6. The pinMode() function configures a pin as either an input or an output. To use it, you pass it the number of the pin to configure and the constant INPUT or OUTPUT. When configured as an input, a pin can detect the state of a sensor like a pushbutton. As an output, it can drive an actuator like an LED. • The digitalWrite() functions outputs a value on a pin. For example, the line: • digitalWrite(ledPin, HIGH); • set the ledPin (pin 13) to HIGH, or 5 volts. Writing a LOW to pin connects it to ground, or 0 volts. • The delay() causes the Arduino to wait for the specified number of milliseconds before continuing on to the next line. There are 1000 milliseconds in a second, so the line: • delay(1000); • creates a delay of one second.

  7. Traffic Light Control • IntledRed= 13; intledGreen = 11;intledYellow = 12; • void setup(){pinMode(ledRed, OUTPUT);      // sets the digital pin as outputpinMode(ledYellow, OUTPUT);      // sets the digital pin as outputpinMode(ledGreen, OUTPUT);      // sets the digital pin as output} • void loop(){digitalWrite(ledGreen, HIGH);   // sets the Green LED on  delay(1000);                  // waits for a seconddigitalWrite(ledGreen, LOW);    // sets the Green LED offdigitalWrite(ledYellow,HIGH);   // sets the Yellow LED on  delay(1000);                  // waits for a seconddigitalWrite(ledYellow, LOW);    // sets the Yellow LED offdigitalWrite(ledRed, HIGH);   // sets the Red LED on  delay(1000);                  // waits for a seconddigitalWrite(ledRed, LOW);    // sets the Reed LED off}

  8. Button • The pushbutton is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button. • We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 10 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 2) which reads the button's state. • When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to 5 volts (through the pull-up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but the resistor in-between them means that the pin is "closer" to ground.)

  9. Button • You can also wire this circuit the opposite way, with a pull-down resistor keeping the input LOW, and going HIGH when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button. • If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will more-or-less randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resister in the circuit.

  10. Button • int ledPin = 13; // choose the pin for the LED • int inPin = 2; // choose the input pin (for a pushbutton) • int val = 0; // variable for reading the pin status • void setup() { • pinMode(ledPin, OUTPUT); // declare LED as output • pinMode(inPin, INPUT); // declare pushbutton as input • } • void loop(){ • val = digitalRead(inPin); // read input value • if (val == HIGH) { // check if the input is HIGH (button released) • digitalWrite(ledPin, LOW); // turn LED OFF • } • else { • digitalWrite(ledPin, HIGH); // turn LED ON • } • }

  11. // ASCII Table // by Nicholas Zambetti <http://www.zambetti.com> void setup() { Serial.begin(9600); Serial.println("ASCII Table ~ Character Map"); // prints title with ending line break delay(100); // wait for the long string to be sent } int number = 33; // first visible character '!' is #33 void loop() { Serial.print(number, BYTE); // prints value unaltered, first will be '!' Serial.print(", dec: "); Serial.print(number); // prints value as string in decimal (base 10) // Serial.print(number, DEC); // this also works Serial.print(", hex: "); Serial.print(number, HEX); // prints value as string in hexadecimal (base 16) Serial.print(", oct: "); Serial.print(number, OCT); // prints value as string in octal (base 8) Serial.print(", bin: "); Serial.println(number, BIN); // prints value as string in binary (base 2) // also prints ending line break // if printed last visible character '~' #126 ... if(number == 126) { // loop forever while(true) { continue; } } number++; // to the next character delay(100); // allow some time for the Serial data to be sent }

  12. ASCII Table

  13. ASCII Table

  14. Setting the baud rate • Serial.begin(int speed) • Description • Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

  15. Sending data to computer • Serial.println(data) • Description • Prints a data to the serial port, followed by a carriage return character(ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print():

  16. Sending data to computer • Serial.println(b) prints b as a decimal number in an ASCII string followed by a carriage return and a linefeed. • Serial.println(b, DEC) prints b as a decimal number in an ASCII string followed by a carriage return and a linefeed. • Serial.println(b, HEX) prints b as a hexadecimal number in an ASCII string followed by a carriage return and a linefeed. • Serial.println(b, OCT) prints b as an octal number in an ASCII string followed by a carriage return and a linefeed. • Serial.println(b, BIN) prints b as a binary number in an ASCII string followed by a carriage return and a linefeed. • Serial.print(b, BYTE) prints b as a single byte followed by a carriage return and a linefeed. • Serial.println(str) if str is a string or an array of chars, prints str an ASCII string. • Serial.println() just prints a carriage return and a linefeed.

  17. char • A data type that takes up 1 byte of memory that stores a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC"). • Characters are stored as numbers however. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers. • The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type. • Example • char myChar = 'A'; • char myChar = 65; // both are equivalent

  18. byte • A byte stores an 8-bit unsigned number, from 0 to 255. • Example • byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal)

  19. int • Integers are your primary datatype for number storage, and store a 2 byte value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). • Int's store negative numbers with a technique called 2's complement math. The highest bit, sometimes refered to as the "sign" bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added. • The Arduino takes care of dealing with negative numbers for you, so that arithmetic operations work transparently in the expected manner. There can be an unexpected complication in dealing with the bitshift right operator (>>) however. • Example • int ledPin = 13; • Syntax • int var = val; • var - your int variable name • val - the value you assign to that variable

  20. unsigned int • Unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1). • The difference between unsigned ints and (signed) ints, lies in the way the highest bit, sometimes refered to as the "sign" bit, is interpreted. In the Arduino int type (which is signed), if the high bit is a "1", the number is interpreted as a negative number, and the other 15 bits are interpreted with 2's complement math. • Example • unsigned int ledPin = 13; • Syntax • unsigned int var = val; • var - your unsigned int variable name • val - the value you assign to that variable

  21. Some variable types • char • byte • int • unsigned int • long • unsigned long • float • double

  22. if (conditional) and ==, !=, <, > (comparison operators) • if, 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 • } • The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code. • The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement. • if (x > 120) digitalWrite(LEDpin, HIGH); • if (x > 120) • digitalWrite(LEDpin, HIGH); • if (x > 120){ digitalWrite(LEDpin, HIGH); } // all are correct

  23. http://www.robotroom.com/HBridge.html Ground

  24. Components: TC4421/TC4422

  25. In the graphic, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example. • analogWrite(pin, value)

  26. int right = 13; // Turn Right int left = 12; // Turn Left int forward = 11; // Move forward int reverse = 10; // Move backward void setup() { pinMode(right, OUTPUT); // sets the digital pin as output pinMode(left, OUTPUT); // sets the digital pin as output } void loop() { digitalWrite(left, LOW); digitalWrite(right, HIGH); // Turn Right analogWrite(reverse, 0); delay(500); analogWrite(forward, 200); // Move forward delay(1000); // waits for a second digitalWrite(right, LOW); digitalWrite(left, HIGH); // Turn left analogWrite(forward, 0); delay(500); analogWrite(reverse, 120); // Move backward delay(1000); // waits for a second }

  27. distance = 32/voltage Good for voltage < 2.6 V, distance > 10 cm

  28. Voltage measurement • int analogRead(pin) • Description • Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. It takes about 100 us (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

  29. Distance Meassurement (Returns # 0 – 1023) • unsigned int voltage1, distance1; • int sensor1 = 0; • void setup() • { • Serial.begin(9600); // setup serial • } • void loop() • { • voltage1 = analogRead(sensor1); • Serial.println(voltage1); // debug value • delay(1000); • }

  30. Car will stop before hitting obstacle • int sensor1 = 0; • int forward = 11; // Move forward • int reverse = 10; // Move backward • int stop_f = 286; • unsigned int voltage1 = 0; • void setup() • { • analogWrite(forward, 0); • analogWrite(reverse, 0); • } • void loop() • { • Serial.println(voltage1); // debug value • while(voltage1 < stop_f) • { • voltage1 = analogRead(sensor1); • analogWrite(forward, 150); // Move forward • } • analogWrite(forward, 0); // Stop • voltage1 = analogRead(sensor1); • }

  31. Control system Car will maintain distance from obstacle

  32. int sensor1 = 0; int forward = 11; // Move forward int reverse = 10; // Move backward int stop_f = 286; // Distance 1 int stop_b = 310; // Distance 2 unsigned int voltage1 = 0; void setup() { analogWrite(forward, 0); analogWrite(reverse, 0); } void loop() { Serial.println(voltage1); // debug value while(voltage1 < stop_f) { voltage1 = analogRead(sensor1); analogWrite(forward, 170); // Move forward } analogWrite(forward, 0); // Stop voltage1 = analogRead(sensor1); while(voltage1 > stop_b) { voltage1 = analogRead(sensor1); analogWrite(reverse, 170); // Move backwards } analogWrite(reverse, 0); // Stop } Distance1= 32/(286*5/1024)=23cm Distance2= 32/(310*5/1024)=21cm

More Related