Show Posts
|
|
Pages: [1] 2
|
|
5
|
Using Arduino / Motors, Mechanics, and Power / Re: Looking for some help in stopping this motorized potentiometer midway
|
on: December 07, 2012, 07:38:18 am
|
Awesome! So my phone died and you can't really see anything on my webcam but pressing the three states on the iPad now works for 0, 512, and 1023. It jitters in the middle now for maybe a second and then it stops. Thank ya! Thank ya! Thank ya! I touched it up a bit to suit my needs. //=====INITIALIZE PINS========
const int PinA1 = 6; // H-bridge leg 1 (pin 2, 1A) PWM const int PinA2 = 5; // H-bridge leg 2 (pin 7, 2A) PWM const int enablePin = 9; // H-bridge enable pin
//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND int lineTrack = A5; //line track for position reading
int inByte = 0; // for incoming serial data
int pos; int potVal; int posRead; //=============================
void setup(){
Serial.begin(57600);
// set the line track as input pinMode(lineTrack, INPUT);
//set the motor logic pins and enable pin as outputs pinMode(PinA1, OUTPUT); pinMode(PinA2, OUTPUT); pinMode(enablePin, OUTPUT); }
void loop() {
potVal = analogRead(lineTrack); posRead = analogRead(lineTrack);
// if we get a valid byte, read analog ins: if (Serial.available() > 0) {
// get incoming byte: inByte = Serial.read();
switch (inByte) { case 'a': motorMoveA(0); break; case 'b': motorMoveA(1023); break; case 'c': motorMoveA(512); break; default: motorStop(); break; } } }
void motorMoveA(int target) { pos; potVal = analogRead(lineTrack); posRead = analogRead(lineTrack);
//Move backwards if potValue less than or equal target if ( potVal > target) { do { posRead = analogRead(lineTrack); int Speed=map (pos,posRead, target, 255,0); analogWrite (enablePin,Speed); analogWrite (PinA1, 180); analogWrite (PinA2, 30); potVal = analogRead(lineTrack); pos = potVal; } while (pos > target); }
//Move forward if potValue less than target if (potVal < target) { do { posRead = analogRead(lineTrack); int Speed=map (pos,posRead, target,255,0); analogWrite (enablePin,Speed); analogWrite (PinA1, 30); analogWrite (PinA2, 180); potVal = analogRead(lineTrack); pos = potVal; } while (pos < target); }
if (potVal == target){ digitalWrite (enablePin,HIGH); digitalWrite (PinA1,HIGH); digitalWrite (PinA2,HIGH); return; } }
void motorStop() { digitalWrite (enablePin,HIGH); digitalWrite (PinA1,HIGH); digitalWrite (PinA2,HIGH); return; }
|
|
|
|
|
6
|
Using Arduino / Motors, Mechanics, and Power / Re: Looking for some help in stopping this motorized potentiometer midway
|
on: December 07, 2012, 05:13:58 am
|
Not sure if I submitted a working code, this one's more efficient. //=====INITIALIZE PINS========
int PinA1 = 6; // H-bridge leg 1 (pin 2, 1A) PWM int PinA2 = 5; // H-bridge leg 2 (pin 7, 2A) PWM int enablePin = 9; // H-bridge enable pin
//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND int lineTrack = A5; //line track for position reading int potVal;
int inByte = 0; // for incoming serial data
// These variables will change: int sensorMin = 1023; // minimum sensor value int sensorMax = 0; // maximum sensor value int sensorValue = 0; // the sensor value //=============================
void setup(){
Serial.begin(57600);
// set the line track as input pinMode(lineTrack, INPUT);
//set the motor logic pins and enable pin as outputs pinMode(PinA1, OUTPUT); pinMode(PinA2, OUTPUT); pinMode(enablePin, OUTPUT); }
void loop() {
potVal = analogRead(lineTrack);
// if we get a valid byte, read analog ins: if (Serial.available() > 0) {
// get incoming byte: inByte = Serial.read();
if (inByte == 97) { moveToTarget1(0); }
if (inByte == 98) { moveToTarget1(100); }
if (inByte == 99) { moveToTarget1(50); } } }
void calibrate() { // read the sensor potVal = analogRead(lineTrack);
// record the maximum sensor value if (sensorValue > sensorMax) { sensorMax = sensorValue; }
// record the minimum sensor value if (sensorValue < sensorMin) { sensorMin = sensorValue; } }
void moveToTarget1(int target) {
int pos; calibrate(); potVal = analogRead(lineTrack); // apply the calibration to the sensor reading potVal = map(potVal, sensorMin, sensorMax, 0, 110); // in case the sensor value is outside the range seen during calibration potVal = constrain(potVal, 0, 110);
if (potVal != target || potVal != target+1 || potVal != target-1){ //Move backwards if potValue less than or equal target if ( potVal > target) { digitalWrite(enablePin, HIGH); analogWrite (PinA1, 150); //Move backwards analogWrite (PinA2, 30); potVal = analogRead(lineTrack); pos = potVal; } //Move forward if potValue less than target if ( potVal < target ) { digitalWrite(enablePin, HIGH); analogWrite (PinA1, 30); //Move forward analogWrite (PinA2, 150); potVal = analogRead(lineTrack); pos = potVal; } } }
|
|
|
|
|
7
|
Using Arduino / Motors, Mechanics, and Power / Re: Looking for some help in stopping this motorized potentiometer midway
|
on: December 07, 2012, 02:47:23 am
|
Very very very very close! So I've gotten it to stop in the middle, except it shakes fervently back and forth in the middle. I think it has to do with conflicting messages in the motor move function. //=====INITIALIZE PINS========
int PinA1 = 6; // H-bridge leg 1 (pin 2, 1A) PWM int PinA2 = 5; // H-bridge leg 2 (pin 7, 2A) PWM int enablePin = 9; // H-bridge enable pin
//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND int lineTrack = A5; //line track for position reading int potVal;
int inByte = 0; // for incoming serial data //=============================
void setup(){
Serial.begin(57600);
// set the line track as input pinMode(lineTrack, INPUT);
//set the motor logic pins and enable pin as outputs pinMode(PinA1, OUTPUT); pinMode(PinA2, OUTPUT); pinMode(enablePin, OUTPUT); }
void loop() {
// set enablePin high so that motor can turn on: potVal = analogRead(lineTrack);
// if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); Serial.println(inByte);
if (inByte == 97) { moveToTarget1(0); }
if (inByte == 98) { moveToTarget1(1023); }
if (inByte == 99) { moveToTarget1(512); } } }
void moveToTarget1(int target) { int pos; potVal = analogRead(lineTrack);
if ( potVal > (target) ) { digitalWrite(enablePin, HIGH); analogWrite (PinA1, 150); //Move backwards analogWrite (PinA2, 30); potVal = analogRead(lineTrack); pos = potVal; }
if ( potVal < (target) ) { digitalWrite(enablePin, HIGH); analogWrite (PinA1, 30); //Move forward analogWrite (PinA2, 150); potVal = analogRead(lineTrack); pos = potVal; }
if ( potVal < target+3 && potVal >target-3) { digitalWrite(enablePin, LOW); //Stop digitalWrite (PinA1, HIGH); digitalWrite (PinA2, HIGH); potVal = analogRead(lineTrack); pos = potVal; } }
|
|
|
|
|
8
|
Using Arduino / Motors, Mechanics, and Power / Re: Looking for some help in stopping this motorized potentiometer midway
|
on: December 06, 2012, 12:00:52 am
|
Are you sure about case 0? This example was built into Arduino: /* Switch statement Demonstrates the use of a switch statement. The switch statement allows you to choose from among a set of discrete values of a variable. It's like a series of if statements. To see this sketch in action, but the board and sensor in a well-lit room, open the serial monitor, and and move your hand gradually down over the sensor. The circuit: * photoresistor from analog in 0 to +5V * 10K resistor from analog in 0 to ground created 1 Jul 2009 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/SwitchCase */
// these constants won't change. They are the // lowest and highest readings you get from your sensor: const int sensorMin = 0; // sensor minimum, discovered through experiment const int sensorMax = 600; // sensor maximum, discovered through experiment
void setup() { // initialize serial communication: Serial.begin(9600); }
void loop() { // read the sensor: int sensorReading = analogRead(A0); // map the sensor range to a range of four options: int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// do something different depending on the // range value: switch (range) { case 0: // your hand is on the sensor Serial.println("dark"); break; case 1: // your hand is close to the sensor Serial.println("dim"); break; case 2: // your hand is a few inches from the sensor Serial.println("medium"); break; case 3: // your hand is nowhere near the sensor Serial.println("bright"); break; } delay(1); // delay in between reads for stability }
|
|
|
|
|
9
|
Using Arduino / Motors, Mechanics, and Power / Re: Looking for some help in stopping this motorized potentiometer midway
|
on: December 05, 2012, 06:41:52 pm
|
I've got it on its way. I made the cases into 0, 1, 2 instead of a, b, c, and am sending those numbers from Processing. Only case 2 (move motor halfway) works, but it shoots all the way forward, and only when I add high output to the enable pin right at the start of void loop(), but when I tried moving it back with my finger it's unable to go past midpoint (b/c I've tried out all the cases on the iPad via TouchOSC). So it's getting the messages but something's up with the enable pin. const int Pot = A5; //Set the Linear Pot middle pin to Arduino A5 port const int Enable = 9; //Set the Motor controller pin on pin 9 Arduino const int pin1A = 6; //Set the Motor Controller pin A1 to port 6; const int pin2A = 5; //Set the Motor Controller pin A2 to port 5; int Target = 0; //Target is the position we want the motor to move to int Speed = 0; //Speed Control for motor void setup() { pinMode (Pot,INPUT); //Set Pot as Input pinMode (Enable,OUTPUT); //Set Enable as Output pinMode (6,OUTPUT); //Set A1 as Output pinMode (5,OUTPUT); //Set A2 as Output Serial.begin(57600); //Initialize Serial Communication at 9600 baud rate }
void loop() { analogWrite(Enable, 255);
if (Serial.available() > 0) { int inByte = Serial.read(); switch (inByte) { case 0: MoveMotor(0); break; case 1: MoveMotor(512); break; case 2: MoveMotor(1023); break;
// default: // MotorStop(); // break; } } }
void MoveMotor(int Target) { int PotVal =analogRead (Pot); if (PotVal > Target) { Speed = map (PotVal,Target,PotVal,0,255); analogWrite (Enable, Speed); digitalWrite (pin1A, HIGH); digitalWrite (pin2A, LOW); } if (PotVal < Target) { Speed = map (PotVal,PotVal,Target,0,255); analogWrite (Enable, Speed); digitalWrite (pin1A, LOW); digitalWrite (pin2A,HIGH); } else { digitalWrite (Enable,HIGH); digitalWrite (pin1A, LOW); digitalWrite (pin2A, LOW); return; } } void MotorStop() { digitalWrite (Enable,HIGH); digitalWrite (pin1A, LOW); digitalWrite (pin2A, LOW); return; }
|
|
|
|
|
12
|
Using Arduino / Motors, Mechanics, and Power / Re: Looking for some help in stopping this motorized potentiometer midway
|
on: December 03, 2012, 06:28:58 pm
|
Yeah, they should open just fine. Here's the code. I've split them up in tabs to keep it a bit organized (thus the zip). So, I figure it might be best to give you context so you're not confused. I have two puppets, Obama and Romney, with this motor as a Pinocchio nose that moves forward or backwards based on the truthfulness of a selected statement. The statements are selected via TouchOSC on an iPad. I've attached my processing code below this one. //=====INITIALIZE PINS========
int switchPin = 7; // switch input int motor1APin = 6; // H-bridge leg 1 (pin 2, 1A) PWM int motor2APin = 5; // H-bridge leg 2 (pin 7, 2A) PWM int enablePin = 9; // H-bridge enable pin
//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND int lineTrack = 5; //line track for position reading int potVal; int prevState;
int incomingByte; // for incoming serial data //=============================
void setup(){
Serial.begin(57600);
// set the switch pin and line track as inputs: pinMode(switchPin, INPUT); pinMode(lineTrack, INPUT);
//set the motor logic pins and enable pin as outputs pinMode(motor1APin, OUTPUT); pinMode(motor2APin, OUTPUT); pinMode(enablePin, OUTPUT);
}
void loop() {
// set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH);
readSerial();
//Cases are named by previous state, followed by new state lieTruth(); truthLie(); truthHalf(); lieHalf(); halfTruth(); halfLie(); }
//If previous state was lie and new state is a truth, move backwards all the way void lieTruth (){ if (prevState == 2 && incomingByte == 65){ // //Obtain position reading // int potVal = analogRead(lineTrack); //read the line positioning // int Speed = map (potVal,potVal,0,255,0); //Map from 1023-0, HIGH-LOW do { // analogWrite (enablePin, Speed); motorBackward(); potVal = analogRead(lineTrack); } while (analogRead(lineTrack) >= 0); if (potVal == 0){ motorStop(); } } }
//If previous state was truth and new state is a lie, move forward all the way void truthLie(){ if (prevState == 1 && incomingByte == 66){ //Obtain position reading // int potVal = analogRead(lineTrack); //read the line positioning // int Speed = map (potVal,potVal,1023,255,0); //Map from 0-1023, HIGH-LOW do { // analogWrite (enablePin, Speed); motorForward(); potVal = analogRead(lineTrack); } while (analogRead(lineTrack) <= 1023); if (potVal == 1023){ motorStop(); } } }
//If prevState was truth and new state half truth. Go forwards halfway void truthHalf(){ if (prevState == 1 && incomingByte == 67){ //Obtain position reading // int potVal = analogRead(lineTrack); //read the line positioning // int Speed = map (potVal,potVal,512,255,0); //Map from 0-512, HIGH-LOW do { // analogWrite (enablePin, Speed); motorForward(); analogRead(lineTrack); } while (analogRead(lineTrack) >= 513); if (potVal == 512){ motorStop(); } } }
//If prevState was lie and new state half truth. Go backwards halfway void lieHalf(){ if (prevState == 2 && incomingByte == 67){ //Obtain position reading // int potVal = analogRead(lineTrack); //read the line positioning // int Speed = map (potVal,potVal,512,255,0); //Map from 1023-512 to HIGH-LOW do { // analogWrite (enablePin, Speed); motorBackward(); analogRead(lineTrack); } while (analogRead(lineTrack) >= 511); if (potVal == 512){ motorStop(); } } }
//If prevState was half-truth and new state truth. Go backwards halfway void halfTruth(){ if (prevState == 3 && incomingByte == 65){ //Obtain position reading // int potVal = analogRead(lineTrack); //read the line positioning // int Speed = map (potVal,potVal,0,255,0); //Map from 512-0, HIGH-LOW do { // analogWrite (enablePin, Speed); motorBackward(); analogRead(lineTrack); } while (analogRead(lineTrack) <= 513); if (potVal == 0){ motorStop(); } } }
//If prevState was half-truth and new state lie. Go forwards halfway void halfLie(){ if (prevState == 3 && incomingByte == 66){ //Obtain position reading // int potVal = analogRead(lineTrack); //read the line positioning // int Speed = map (potVal,potVal,1023,255,0); //Map from 512-1023, HIGH-LOW do { // analogWrite (enablePin, Speed); motorForward(); analogRead(lineTrack); } while (analogRead(lineTrack) >= 511); if (potVal == 1023){ motorStop(); } } }
void motorForward(){ analogWrite(motor1APin, 30); analogWrite(motor2APin, 160); }
void motorBackward(){ analogWrite(motor1APin, 160); analogWrite(motor2APin, 30); }
void motorStop(){ analogWrite(motor1APin, 255); analogWrite(motor2APin, 255); }
void readSerial(){
int prevState; //Allow previous states to be stored here in prevState // 1 = truth // 2 = lie // 3 = half-truth
// read the incoming byte data from Processing: if (Serial.available() > 0) { incomingByte = Serial.read(); }
//Store data as previous states if (incomingByte == 65) { //Truth prevState = 1; } else if(incomingByte == 66) { //Lie prevState = 2; } else if(incomingByte == 67) { //Half-truth prevState = 3; }
} Processing code: //libraries needed for arduino communication import processing.serial.*;
//libraries needed for osc import oscP5.*; import netP5.*;
Serial arduinoPort; // Set arduinoPort as serial connection
OscP5 oscP5; NetAddress myRemoteLocation;
//set/change port numbers here int incomingPort = 8000; int outgoingPort = 9000;
//set/change the IP address that the OSC data is being sent to //127.0.0.1 is the local address (for sending osc to an application on the same computer) String ipAddress = "127.0.0.1";
int truthVal; //truthValue
void setup() { size(100, 100); background(0); frameRate(25);
/* start oscP5, listening for incoming messages at port 8000 */ //for INCOMING osc messages (e.g. from Max/MSP) oscP5 = new OscP5(this, incomingPort); //port number set above
arduinoPort = new Serial(this, Serial.list()[4], 57600); //port 4 is Arduino to USB }
// Incoming osc message are forwarded to the following oscEvent method. Write to the arduino here-------- // This method is called for each OSC message recieved void oscEvent(OscMessage theOscMessage) {
println("### received an osc message."); println("addrpattern: "+theOscMessage.addrPattern());
//Convert the OSC address pattern to a string String addr = theOscMessage.addrPattern();
if (addr.equals("/o/truth") == true) //if the osc message reads obama truth { truthVal = 0; //Set truthVal to 0 }
else if (addr.equals("/o/lie") == true) //if the osc message reads obama lie { truthVal = 1; //Set truthVal to 1 } else if (addr.equals("/o/halfTruth") == true) { truthVal = 2; } }
void draw() { //write serial data to for the Arduino to read if (truthVal == 0) //if the osc message reads obama truth { arduinoPort.write(65); println("Obama Truth"); } else if (truthVal == 1) //if the osc message reads obama truth { arduinoPort.write(66); println("Obama Lie"); } else if (truthVal == 2) //if the osc message reads obama truth { arduinoPort.write(67); println("Obama Half-Truth"); } }
|
|
|
|
|