Arduino Digital Out_QUICK RECAP
BLINK File> Examples>Digital>Blink int ledpin = 13; // LED connected to digital pin 13 // The setup() method runs once, when the sketch starts void setup() // initialize the digital pin as an output: pinmode(ledpin, OUTPUT); // the loop() method runs over and over again, // as long as the Arduino has power void loop() digitalwrite(ledpin, HIGH); // set the LED on delay(1000); // wait for a second or 1000 milliseconds digitalwrite(ledpin, LOW); // set the LED off delay(1000); // wait for a second or 1000 milliseconds
Arduino Digital In and Out
Pull Up Resistor A pull-up resistor weakly pulls the voltage of the wire it is connected to towards its voltage source level when the other components on the line are inactive. When all other connections on the line are inactive, they are highimpedance and act like they are disconnected Arduino will read HIGH until circuit is activated and it will switch to LOW. Pull Down Resistor A pull-down resistor is connected to ground, and holds the logic signal near zero volts when no other active device is connected. Arduino will read LOW until circuit is activated and it will switch to HIGH.
FILE> Examples>Digital>Button DIGITAL IN_BUTTON int buttonpin = 2; // the number of the pushbutton pin int ledpin = 13; // the number of the LED pin int buttonstate = 0; // variable for reading the pushbutton status void setup() // initialize the LED pin as an output: pinmode(ledpin, OUTPUT); // initialize the pushbutton pin as an input: pinmode(buttonpin, INPUT); void loop() // read the state of the pushbutton value: buttonstate = digitalread(buttonpin); // check if the pushbutton is pressed. // if it is, the buttonstate is HIGH: if (buttonstate == HIGH) // turn LED on: digitalwrite(ledpin, HIGH); else // turn LED off: digitalwrite(ledpin, LOW);
ARDUINO_Internal Pull Up Resistor int buttonpin = 2; // the number of the pushbutton pin int ledpin = 13; // the number of the LED pin int buttonstate = 0; // variable for reading the pushbutton status void setup() // initialize the LED pin as an output: pinmode(ledpin, OUTPUT); // initialize the pushbutton pin as an input: pinmode(buttonpin, INPUT); //turns on the pullup resistor on buttonpin digitalwrite(buttonpin, HIGH); arduino 1.0.1: void setup() // initialize the LED pin as an output: pinmode(ledpin, OUTPUT); // initialize the pushbutton pin as an input: pinmode(buttonpin, INPUT_PULLUP ); void loop() // read the state of the pushbutton value: buttonstate = digitalread(buttonpin); // check if the pushbutton is pressed. // if it is, the buttonstate is HIGH: if (buttonstate == HIGH) // turn LED on: digitalwrite(ledpin, HIGH); else // turn LED off: digitalwrite(ledpin, LOW);
Arduino Analog In and Out
DIGITAL AND ANALOG I/O ON THE BOARD A OUT A OUT DIGITAL I/O A O U T ANALOG IN
ANALOG DIGITAL CONVERTERS (ADCs) _ADC is an electronic device that converts an input analog voltage (or current) to a digital number proportional to the magnitude of the voltage or curren _Arduino has a built in 10-bit ADC which keeps checking the value of the voltage input and returns back the values. It takes (0.0001 s) per reading. 10,000 readings / sec.
Pulse Width Modulation(PWM) _Digital way of getting an analog current supply. _ a PWM variable-power scheme switches the power quickly between fully on and fully off _Pulse: sudden increase and decrease in current flow. _ The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time _Low (0-255) High
DIGITAL I/O pinmode(pin#, INPUT/OUTPUT); //assigns pin as input or output digitalwrite(pin#, STATE: HIGH/LOW); //OUTPUT digitalread(pin#); //INPUT ANALOG I/O analogwrite(pin#, the duty cycle: 0 (always off)-255 (always on)); //OUTPUT analogread(pin#); //INPUT
ANALOG OUT_PWM int analogpin = 9; void setup() Serial.begin(9600); void loop() analogwrite(analogpin, 0); //0% delay(1000); analogwrite(analogpin, 64); //25% delay(1000); analogwrite(analogpin, 127); //50% delay(1000); analogwrite(analogpin, 191); //75% delay(1000); analogwrite(analogpin, 255); //100% delay(1000);
Voltage Divider How is it used with Arduino? point between the fixed pulldown or pullup resistor and the variable resistor is connected to the analog input of a microcontroller Why do you need it? fixed resistor scales the values of variable resistor by referencing two voltages: 5V and 0V. fixed resistor Values are LOW to HIGH 0-255 variable resistor
ANALOG IN_variable resistor FILE> Examples> Analog> AnalogInput int sensorpin = 0; // select the input pin for the potentiometer int ledpin = 9; // select the PWM pin for the LED int sensorvalue = 0; // variable to store the value coming from the sensor void setup() Serial.begin(9600); void loop() // read the value from the sensor sensorvalue = analogread(sensorpin); //prints sensor value to program notification area using Serial.print or Serial.println Serial.println(sensorValue); delay(500); // turn the ledpin on according to potentiometer s modulation analogwrite(ledpin, sensorvalue);
int sensorpin = 0; int ledpin = 9; Scaling Values_Software // select the input pin for the potentiometer // select the PWM pin for the LED int sensorvalue = 0; // variable to store the value coming from the sensor int outputdata = 0; // Assigning integer value to the variable outputdata void setup() Serial.begin(9600); void loop() // read the value from the sensor sensorvalue = analogread(sensorpin); // map it to the range of the analog out outputdata = map(sensorvalue, 0, 1023, 0, 255); // turn the ledpin on according to potentiometer s modulation analogwrite(ledpin, sensorvalue);
SERVOS
SERVO TYPES Regular Servo: _Can only rotate from 0-180 _position can be tracked by rotation angle _direction can be controlled by incrementing or descreasing rotation angle. Continous Rotation: _lose position tracking _gain 0-360 rotation, speed, and direction
FILE> Examples>Servo> Sweep SERVOS_SWEEP #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 ++) //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--) // 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
SERVOS_using variable resistor http://www.arduino.cc/playground/componentlib/servo #include <Servo.h> Servo myservo; //create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val = 0; // variable to read the value from the analog pin void setup() myservo.attach(2); // attaches the servo on pin 2 to the servo object void loop() val = analogread(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there
SERIAL COMMUNICATION Bits, Bytes, Data Rates and Protocols ASCI interpretation Using terminal to view serial Data Serial Out from Arduino Serial In to Processing, PD, Max/MSP, Flash
SERIAL COMMUNICATION _creates a data stream by sending one bit at a time _occurs sequentially H...E...L...L...O The first to be recieved is H then E and so on.
SERIAL COMMUNICATION_ARDUINO RX Receiving Data TX Transmitting Data
Time to Talk with Arduino board! _Program to let Arduino say Hello World! _Baud Rate is the rate at which data is sent and recieve, must be synced between the two devices that are communicating _Bit per second (baud rate) _Note that this communication baud rate is independent of the upload process, which is fixed at 19200 bps.
Sending Serial Data Serial.println( something ); print and println is writing data to the serial port. println writes a \n(newline) at the end of each statement equivalant to a serial.write(); in other programming langauages
int val = 0; void setup() Serial.begin(9600); void loop() val = analogread(0); Serial.print(val); Serial.println(); Sending Serial Data to Processing IDE PROCESSING CODE: import processing.serial.*; Serial myport; String instring; float val; int lf = 10; void setup() size(200, 200); String portname = Serial.list()[0]; myport = new Serial(this, portname, 9600); myport.clear(); void draw() // Create object from Serial class // Data received from the serial port // ascii code for linefeed background(255); // Set background to white val = map(val, 0, 1023, 0, 255); fill(int(val)); //typecast float to int rect(50, 50, 100, 100); /*built in function within processing called when data is available which can be set with buffer() to only trigger after a certain number of data elements are read and can be set with bufferuntil() to only trigger after a specific character is read to be used with one of the Read() functions*/ void serialevent(serial p) myport.bufferuntil(lf); instring = p.readstring(); print( received: +instring); val = float(instring); println( val); //buffer until linefeed (println) //typecast string type to float
Arduino_receiving data via Serial.Available int incomingbyte = 0; // for incoming serial data void setup() Serial.begin(9600); // opens serial port, sets data rate to 9600 bps void loop() // send data only when you receive data: if (Serial.available() > 0) // read the incoming byte: incomingbyte = Serial.read(); // say what you got: Serial.print( I received: ); Serial.println(incomingByte); else Serial.println( Nothing );
SERIAL READ_switch case Code: http://arduino.cc/en/tutorial/switchcase2
EXAMPLES > CONTROL> SWITCHCASE2 void setup() // initialize serial communication: Serial.begin(9600); // initialize the LED pins: for (int thispin = 2; thispin < 7; thispin++) pinmode(thispin, OUTPUT); SERIAL READ_switch case void loop() // read the sensor: if (Serial.available() > 0) int inbyte = Serial.read(); /*do something different depending on the character received. The switch statement expects single number values for each case; in this exmaple, though, you re using single quotes to tell the controller to get the ASCII value for the character. For example a = 97, b = 98, and so forth:*/ switch (inbyte) case a : digitalwrite(2, HIGH); break; case b : digitalwrite(3, HIGH); break; case c : digitalwrite(4, HIGH); break; case d : digitalwrite(5, HIGH); break; case e : digitalwrite(6, HIGH); break; default: // turn all the LEDs off: for (int thispin = 2; thispin < 7; thispin++) digitalwrite(thispin, LOW);
SERIAL FLUSH Flushes the buffer of serial port, when sending or recieving any call to Serial method will return only data received after all the most recent call to Serial.flush(): Serial.println( Hello! ); Serial.flush(); will pass Hello! \n to Serial buffer before doing anything else to the Serial buffer.