1 / 17

Arduino 1

Arduino 1. Basics. Comments

penda
Download Presentation

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 • 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.

  4. 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.

  5. 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}

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

  7. Components: TC4421/TC4422

  8. 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)

  9. 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 }

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

  11. 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.

  12. 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.

  13. 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():

  14. 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.

  15. 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); • }

  16. int sensor1 = 0; • int forward = 11; // Move forward • int reverse = 10; // Move backward • int stop_f = 286; • unsigned int voltage1 = 0; • void setup() • { • Serial.begin(9600); // setup serial • } • void loop() • { • analogWrite(reverse, 0); • // analogWrite(forward, 0); • Serial.println(voltage1); // debug value • while(voltage1 < stop_f) • { • voltage1 = analogRead(sensor1); • analogWrite(forward, 200); // Move forward • Serial.println(voltage1); // debug value • } • analogWrite(forward, 0); // Move forward • }

More Related