Interactive Workshop for Artists & Designers Earl Park
Motor
Servo Motor Control #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin int ang; myservo.attach(9); // attaches the servo on pin 9 to the servo object val = analogread(potpin); //reads the value of the potentiometer ang = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) myservo.write(ang); // sets the servo position according to the scaled value delay(10); // waits for the servo to get there
const int enablepin = 6; // H-bridge enable pin const int motor1pin = 7; // H-bridge leg 1 (pin 2, 1A) const int motor2pin = 8; // H-bridge leg 2 (pin 7, 2A) Serial.begin(9600); Serial.println("+ - to set direction, any other key stops motor"); // set all the other 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); if( Serial.available()) { char ch = Serial.read(); if(ch == '+'){ Serial.println("cw"); digitalwrite(motor1pin, LOW); // set leg 1 of the H-bridge low digitalwrite(motor2pin, HIGH); // set leg 2 of the H-bridge high else if(ch == '-'){ Serial.println("ccw"); digitalwrite(motor1pin, HIGH); // set leg 1 of the H-bridge high digitalwrite(motor2pin, LOW); // set leg 2 of the H-bridge low else{ Serial.println("stop motor"); digitalwrite(motor1pin, LOW); // set leg 1 of the H-bridge low digitalwrite(motor2pin, LOW); // set leg 2 of the H-bridge low
const int enablepin = 6; // H-bridge enable pin const int motor1pin = 7; // H-bridge leg 1 (pin 2, 1A) const int motor2pin = 8; // H-bridge leg 2 (pin 7, 2A) Serial.begin(9600); Serial.println("+ - to set direction, any other key stops motor"); pinmode(motor1pin, OUTPUT); pinmode(motor2pin, OUTPUT); pinmode(enablepin, OUTPUT); if( Serial.available()) { char ch = Serial.read(); if (isdigit (ch)){ // if ch is a number... int speed = map(ch, '0', '9', 0, 255); analogwrite(enablepin, speed); Serial.println(speed); else if(ch == '+'){ Serial.println("cw"); digitalwrite(motor1pin, LOW); // set leg 1 of the H-bridge low digitalwrite(motor2pin, HIGH); // set leg 2 of the H-bridge high else if(ch == '-'){ Serial.println("ccw"); digitalwrite(motor1pin, HIGH); // set leg 1 of the H-bridge high digitalwrite(motor2pin, LOW); // set leg 2 of the H-bridge low else{ Serial.println("Unexpected character"); Serial.println(ch);
const int switchpin = 2; // switch input const int enablepin = 6; // H-bridge enable pin const int motor1pin = 7; // H-bridge leg 1 (pin 2, 1A) const int motor2pin = 8; // H-bridge leg 2 (pin 7, 2A) // set the switch as an input: pinmode(switchpin, INPUT); // set all the other 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); // if the switch is high, motor will turn on one direction: if (digitalread(switchpin) == HIGH) { digitalwrite(motor1pin, LOW); // set leg 1 of the H-bridge low digitalwrite(motor2pin, HIGH); // set leg 2 of the H-bridge high else { // if the switch is low, motor will turn in the other direction: digitalwrite(motor1pin, HIGH); // set leg 1 of the H-bridge high digitalwrite(motor2pin, LOW); // set leg 2 of the H-bridge low
const int switchpin = 2; // switch input const int potpin = 5; // analog input const int enablepin = 6; // H-bridge enable pin const int motor1pin = 7; // H-bridge leg 1 (pin 2, 1A) const int motor2pin = 8; // H-bridge leg 2 (pin 7, 2A) // set the switch as an input: pinmode(switchpin, INPUT); // set all the other pins you're using as outputs: pinmode(motor1pin, OUTPUT); pinmode(motor2pin, OUTPUT); pinmode(enablepin, OUTPUT); int speed = analogread(potpin) / 4; boolean reverse = digitalread(switchpin); setmotor(speed, reverse); void setmotor(int speed, boolean reverse) { analogwrite(enablepin, speed); digitalwrite(motor1pin,! reverse); digitalwrite(motor2pin, reverse);
const int leftsensorpin = 0; // analog input const int rightsensorpin = 1; // analog input int leftpins[] = {6, 7, 8, rightpins[] = {11, 10, 9; int Min_pwm = 32, Max_pwm = 255; int sensorthreshold = 0; Serial.begin(9600); for (int i = 0; i < 3; i++) { pinmode(leftpins[i], OUTPUT); pinmode(rightpins[i], OUTPUT); int leftval = analogread(leftsensorpin); int rightval = analogread(rightsensorpin); if (sensorthreshold == 0) { sensorthreshold = ((leftval + rightval) / 2 + 100); setspeed(leftpins, leftval, map(leftval, 0, 1023, Min_pwm, Max_pwm)); setspeed(rightpins, rightval, map(rightval, 0, 1023, Min_pwm, Max_pwm)); Serial.print(leftVal); Serial.print(" + "); Serial.print(sensorThreshold); Serial.print(" + "); Serial.println(rightVal); void setspeed(int pins[], int val, int speed) { if (sensorthreshold < val) { digitalwrite(pins[1], HIGH); digitalwrite(pins[2], LOW); else { digitalwrite(pins[1], LOW); digitalwrite(pins[2], HIGH); analogwrite(pins[0], speed);