Arduino Circuits and Code. int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() {...
-
Upload
toby-spencer -
Category
Documents
-
view
274 -
download
0
Transcript of Arduino Circuits and Code. int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() {...
Arduino Circuits and Code
int ledPin = 9; void setup() pinMode(ledPin, OUTPUT);
void loop() digitalWrite(ledPin, LOW); delay(1000); digitalWrite(ledPin, HIGH); delay(1000); for (int i = 0; i<=255; i++) analogWrite(ledPin, i); delay(10);
power
led
groundresistor
digital and analog write(actuator - led)
• it matters which way round you put the ledlong leg to positive
• Resitance = (VS - VL) / IVS = supply voltage , VL = LED voltage, I = LED
current
• pwm pins for analogWrite
things to remember
digital read(sensor - button)
int inputPin = 9; int val = 0;
void setup() pinMode(inputPin, INPUT); Serial.begin(9600);
void loop() val = digitalRead(inputPin); Serial.println(val);
5Vbutton
ground
pin 9
10kΩ
• use serial prints for debugging
• resistor needed in circuit
• debounce
things to remember
analog read(sensor - ldr)
int inputPin = 5; int val = 0;
void setup() Serial.begin(9600);
void loop() val = analogRead(inputPin); Serial.println(val);
ldr
5V
ground
pin5
analog read(sensor - potentiometer)
• values 0 to 1023 - use map to get 0 to 255
• use millis() instead of delay
things to remember
servos
#include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; void setup() myservo.attach(9); // attaches pin 9 to the servo object void loop() for(pos = 0; pos < 180; pos += 1) myservo.write(pos); delay(15); for(pos = 180; pos>=1; pos-=1) myservo.write(pos); delay(15);
servos
• common rotation range is 180º
• good for smooth continuous motion
• servo library uses pins 9 and 10
• servo needs time to reach destination
steppers
steppers
#include <Stepper.h>
// change this to the number of steps on your motor#define STEPS 100
Stepper stepper(STEPS, 4, 5, 6, 7);
void setup() // set the speed of the motor to 30 RPMs stepper.setSpeed(30);
void loop() stepper.step(100); delay(1000);
*
stepper
• can rotate 360º
• can lock into fixed position - strong torque
• precise position setting
• stepper library
dc motors
dc motors
const int motor1Pin = 3; const int motor2Pin = 4; const int enablePin = 9;
void setup()
// set all the pins you're using as outputs: pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); pinMode(enablePin, OUTPUT);
// set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH); //to control the speed of the motor //analogWrite(enablePin, 30)
void loop() digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high // swap HIGH and LOW values to change motor direction // set both HIGH or both LOW to stop motor
dc motors
• torque
• gears
• direction and speed
solenoids
solenoid
boolean drum[] = 1, 0, 0, 0, 0, 1, 0, 1;int drumPin = 2;int counter;
void setup() pinMode(drumPin, OUTPUT); counter = 0;void loop() digitalWrite(drumPin, drum[counter]);
if(counter == 7) counter = 0; else counter++; delay(200); // delay between beats
solenoids
• push and pull - use springs
• toy hacking - reed relays