unnamed

Tonight I worked on the electronics for my cell sculpture.  The DC vibration motor is controlled by the adafruit motor shield (v2).  The motor will be triggered by a pushbutton to turn it on for 10-15 seconds, its’ overall speed controlled by a potentiometer.  The button will also cause a white LED strip (backlighting) to fade in, then fade out after the motor finishes.  Here’s what the wiring looks like (DC motor not pictured):

fritzing

Here is the arduino code:

//For use with the Adafruit Motor Shield v2
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which ‘port’ M1, M2, M3 or M4. In this case, M2
Adafruit_DCMotor *myMotor = AFMS.getMotor(2);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

int potPin = A2;
int inPin = 1;
int val = 0;

#define DELAY 3
#define PIN 11

void setup() {

pinMode(PIN, OUTPUT);
pinMode(inPin, INPUT_PULLUP); // declare pushbutton as input

AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(0);
myMotor->run(FORWARD);

// turn on motor

}

void loop() {
uint8_t i;
val = digitalRead(inPin); // read input value
if (val == LOW) {
myMotor->setSpeed(LOW);
myMotor->run(RELEASE);

} else {
delay(50);
// fade in
for(int i=0; i<255; i++) {
analogWrite(PIN, i);
delay(DELAY);
}

myMotor->run(FORWARD);
myMotor->setSpeed(analogRead(potPin)/4);
delay (5000);

// fade out
for(int i=0; i<255; i++) {
analogWrite(PIN, 255-i);
delay(DELAY);
}

}

}

Advertisements