ARDUINO 1
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. * * */ // LED connected to digital pin 13
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.
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.
Traffic Light Control Int ledRed = 13; int ledGreen = 11; int ledYellow = 12; void setup() { pinMode(ledRed, OUTPUT); // sets the digital pin as output pinMode(ledYellow, OUTPUT); // sets the digital pin as output pinMode(ledGreen, OUTPUT); // sets the digital pin as output } void loop() { digitalWrite(ledGreen, HIGH); // sets the Green LED on delay(1000); // waits for a second digitalWrite(ledGreen, LOW); // sets the Green LED off digitalWrite(ledYellow,HIGH); // sets the Yellow LED on delay(1000); // waits for a second digitalWrite(ledYellow, LOW); // sets the Yellow LED off digitalWrite(ledRed, HIGH); // sets the Red LED on delay(1000); // waits for a second digitalWrite(ledRed, LOW); // sets the Reed LED off }
Ground
Components: TC4421/TC4422
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 , 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 analogWrite(pin, value)
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 }
distance = 32/voltage Good for voltage < 2.6 V, distance > 10 cm
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 This yields a resolution between readings of: 5 volts / 1024 units or,.0049 volts (4.9 mV) per unit. It takes about 100 us ( s) to read an analog input, so the maximum reading rate is about 10,000 times a second.
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 You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.
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():
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.
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); }
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 }