Εισαγωγή στην Μηχατρονική Τμήμα Μηχανολόγων Μηχανικών Τ.Ε. Τ.Ε.Ι. Κρήτης Παραδείγµατα χρήσης του µικροελεγκτή Arduino Εφαρµογές για το εργαστήριο Μέρος 1 ο Δρ. Φασουλάς Γιάννης, Επίκ. Καθηγητής Νοέμβριος 2014 1
Παραδείγµατα µπορείτε να βρείτε στην διεύθυνση http://www.arduino.cc/en/tutorial/homepage Παράδειγμα 1: Blink http://arduino.cc/en/tutorial/blink Blink Turns on an LED on for one second, then off for one second, repeatedly. int led = 13; // Pin 13 has an LED connected on most Arduino boards, give it a name: void setup() pinmode(led, OUTPUT); void loop() digitalwrite(led, HIGH); delay(1000); digitalwrite(led, LOW); delay(1000); // the setup routine runs once when you press reset: // initialize the digital pin as an output. // the loop routine runs over and over again forever: // turn the LED on (HIGH is the voltage level) // wait for a second // turn the LED off by making the voltage LOW // wait for a second 2
Παράδειγμα 2: Button http://www.arduino.cc/en/tutorial/button Button Turns on and off a light emitting diode(led) connected to digital pin 13, when pressing a pushbutton attached to pin 2. * Note: on most Arduinos there is already an LED on the board attached to pin 13. // constants won't change. They're used here to set pin numbers: const int buttonpin = 2; // the number of the pushbutton pin const int ledpin = 13; // the number of the LED pin // variables will change: int buttonstate = 0; // variable for reading the pushbutton status void setup() pinmode(ledpin, OUTPUT); pinmode(buttonpin, INPUT); // initialize the LED pin as an output: // initialize the pushbutton pin as an input: void loop() buttonstate = digitalread(buttonpin); // read the state of the pushbutton value: if (buttonstate == HIGH) // check if the pushbutton is pressed. digitalwrite(ledpin, HIGH); // if it is, the buttonstate is HIGH, turn LED on: else digitalwrite(ledpin, LOW); // turn LED off: 3
Παράδειγμα 3: Digital Read to Serial http://arduino.cc/en/tutorial/digitalreadserial Reads a digital input on pin 2, prints the result to the serial monitor int pushbutton = 2; // digital pin 2 has a pushbutton attached to it. Give it a name: void setup() // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinmode(pushbutton, INPUT); // make the pushbutton's pin an input: void loop() int buttonstate = digitalread(pushbutton); // read the input pin: Serial.println(buttonState); // print out the state of the button: delay(1); // delay in between reads for stability 4
Παράδειγμα 4: Analog Read to Serial http://arduino.cc/en/tutorial/analogreadserial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. void setup() Serial.begin(9600); // initialize serial communication at 9600 bits per second: void loop() int sensorvalue = analogread(a0); // read the input on analog pin 0: Serial.println(sensorValue); // print out the value you read and change line: delay(1); // delay in between reads for stability 5
Παράδειγμα 5: Fading - PWM http://arduino.cc/en/tutorial/fade This example shows how to fade an LED on pin 9 using the analogwrite() function. int led = 9; int brightness = 0; int fadeamount = 5; // the pin that the LED is attached to // how bright the LED is // how many points to fade the LED by void setup() pinmode(led, OUTPUT); // declare pin 9 to be an output: void loop() analogwrite(led, brightness); // set the brightness of pin 9: brightness = brightness + fadeamount; // change the brightness for next time through the loop if (brightness == 0 brightness == 255) // reverse the direction of the fading at the ends of //the fade fadeamount = -fadeamount ; delay(30); // wait for 30 milliseconds to see the dimming effect 6
Παράδειγμα 6: Analog Read Voltage http://arduino.cc/en/tutorial/readanalogvoltage ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. void setup() Serial.begin(9600); // initialize serial communication at 9600 bits per second: void loop() int sensorvalue = analogread(a0); // read the input on analog pin A0 float voltage = sensorvalue * (5.0 / 1024.0); // compute Q (analog quantization size) Serial.println(voltage); // print out the value you read: 7
Παράδειγμα 7: RGB LED, Color Fade Cycle RGB_LED_Color_Fade_Cycle.pde // LED leads connected to PWM pins const int RED_LED_PIN = 9; const int GREEN_LED_PIN = 10; const int BLUE_LED_PIN = 11; // Used to store the current intensity level of the individual LEDs int redintensity = 0; int greenintensity = 0; int blueintensity = 0; // Length of time we spend showing each color const int DISPLAY_TIME = 100; // In milliseconds void setup() // No setup required. void loop() // Cycle color from red through to green // (In this loop we move from 100% red, 0% green to 0% red, 100% green) for (greenintensity = 0; greenintensity <= 255; greenintensity+=5) redintensity = 255-greenIntensity; analogwrite(green_led_pin, greenintensity); analogwrite(red_led_pin, redintensity); delay(display_time); // Cycle color from green through to blue // (In this loop we move from 100% green, 0% blue to 0% green, 100% blue) for (blueintensity = 0; blueintensity <= 255; blueintensity+=5) greenintensity = 255-blueIntensity; analogwrite(blue_led_pin, blueintensity); analogwrite(green_led_pin, greenintensity); delay(display_time); // Cycle cycle from blue through to red // (In this loop we move from 100% blue, 0% red to 0% blue, 100% red) for (redintensity = 0; redintensity <= 255; redintensity+=5) blueintensity = 255-redIntensity; analogwrite(red_led_pin, redintensity); analogwrite(blue_led_pin, blueintensity); delay(display_time); 8
Παράδειγμα 8: Έλεγχος κινητήρα RC SERVO http://ardx.org/src/circ/circ04-code.txt #include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() myservo.attach(9); // attaches the servo on pin 9 to the servo object void loop() for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position 9
Παράδειγμα 9: Έλεγχος κινητήρα RC SERVO με τη χρήση ποτενσιομέτρου // Controlling a servo position using a potentiometer (variable resistor) #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; int val; void setup() myservo.attach(9); // attaches the servo on pin 9 to the servo object void loop() val = analogread(potpin); val = map(val, 0, 1023, 0, 179); myservo.write(val) delay(15); 10
Παράδειγμα 10: Οδήγηση Ρελέ int ledpin = 2; void setup() pinmode(ledpin, OUTPUT); void loop() digitalwrite(ledpin, HIGH); delay(1000); digitalwrite(ledpin, LOW); delay(1000); 11
Παράδειγμα 11: Έλεγχος κινητήρα DC μόνιμου μαγνήτη, http://ardx.org/src/circ/circ03-code.txt int motorpin = 9; void setup() pinmode(motorpin, OUTPUT); void loop() motoronthenoff(); //motoronthenoffwithspeed(); //motoracceleration(); void motoronthenoff() int ontime = 2500; int offtime = 1000; digitalwrite(motorpin, HIGH); delay(ontime); digitalwrite(motorpin, LOW); delay(offtime); milliseconds void motoronthenoffwithspeed() int onspeed = 200; int ontime = 2500; int offspeed = 50; int offtime = 1000; analogwrite(motorpin, onspeed); delay(ontime); analogwrite(motorpin, offspeed); delay(offtime); void motoracceleration() int delaytime = 50; //Accelerates the motor for(int i = 0; i < 256; i++) analogwrite(motorpin, i); delay(delaytime); //Decelerates the motor for(int i = 255; i >= 0; i--) analogwrite(motorpin, i); delay(delaytime); 12
Παράδειγμα 12: Έλεγχος κινητήρα με το motor shield void setup() pinmode(12, OUTPUT); //DIRECTION Motor Channel A pinmode(9, OUTPUT); // BRAKE Motor Channel A void loop() //forward @ full speed digitalwrite(12, HIGH); //Establishes forward direction of Channel A digitalwrite(9, LOW); //Disengage the Brake for Channel A analogwrite(3, 255); //Spins the motor on Channel A at full speed delay(3000); digitalwrite(9, HIGH); //Eengage the Brake for Channel A delay(1000); //backward @ half speed digitalwrite(12, LOW); //Establishes backward direction of Channel A digitalwrite(9, LOW); //Disengage the Brake for Channel A analogwrite(3, 123); //Spins the motor on Channel A at half speed delay(3000); digitalwrite(9, HIGH); //Eengage the Brake for Channel A delay(1000); Summary Operating Voltage Motor controller Max current Current sensing Free running stop and brake function Function Direction Speed (PWM) Brake Current Sensing 5V to 12V L298P, Drives 2 DC motors or 1 stepper motor 2A per channel or 4A max (with external power supply) 1.65V/A, AD converter in 3V3 for max current 2A Channel A Digital 12 Digital 3 Digital 9 Analog 0 Vin: τάση τροφοδοσίας κινητήρα µέχρι 12 volt, Channel B Digital 13 Digital 11 Digital 8 Analog 1 Gnd: Γείωση κινητήρα 13
Παράδειγμα 13: Έλεγχος φωτεινότητας με Φωτοαντίσταση LDR * A simple programme that will change the intensity of * an LED based * on the amount of light incident on * the photo resistor. * //PhotoResistor Pin int lightpin = 0; //the analog pin the photoresistor is //connected to //the photoresistor is not calibrated to any units so //this is simply a raw sensor value (relative light) //LED Pin int ledpin = 9; //the pin the LED is connected to //we are controlling brightness so //we use one of the PWM (pulse width // modulation pins) void setup() pinmode(ledpin, OUTPUT); //sets the led pin to output void loop() int lightlevel = analogread(lightpin); //Read the // lightlevel lightlevel = map(lightlevel, 0, 900, 0, 255); //adjust the value 0 to 900 to //span 0 to 255 lightlevel = constrain(lightlevel, 0, 255);//make sure the //value is betwween //0 and 255 analogwrite(ledpin, lightlevel); //write the value Pin 9 14
Παράδειγμα 14: Έλεγχος θερμοκρασίας με αναλογικό αισθητήρα Σε αυτή την εργαστηριακή άσκηση χρησιμοποιούμε το αισθητήριο θερμοκρασίας TMP36 (Precision Temperature Sensor) το οποίο διαθέτει μία αναλογική έξοδο και ευαισθησία 10 m volt/c o δ καθώς επίσης λειτουργεί με μία αρχική αντιστάθμιση (offset) των 500m volt προκειμένου να παρέχεται η δυνατότητα μέτρησης και αρνητικών θερμοκρασιών. int temperaturepin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to //the resolution is 10 mv / degree centigrade //(500 mv offset) to make negative temperatures an option void setup() Serial.begin(9600); //Start the serial connection with the copmuter //to view the result open the serial monitor //last button beneath the file bar (looks like a box with an antenae) void loop() // run over and over again float temperature = getvoltage(temperaturepin); //getting the voltage reading from the //temperature sensor temperature = (temperature 0.5) * 100; //converting from 10 mv per degree with 500 mv offset //to degrees ((volatge - 500mV) times 100) Serial.println(temperature); //printing the result delay(1000); //waiting a second * getvoltage() - returns the voltage on the analog input defined by * pin float getvoltage(int pin) return (analogread(pin) *.004882814); //converting from a 0 to 1023 digital range // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts Ευαισθησία οργάνου: 10 mvolt/c o Υπάρχει ένα αρχικό offset: 500mvolt Μέγεθος αναλογικής βαθµιδοποίησης: Q=(5-0)/1024=0.004882814 15