Hey completely new, Arduino code completely over my head so far
Im working on a sculpture project and I am trying to control a dc motor direction with a potentiometer. I dont care about speed I just want to make it change directions by twisting the pot left or right. I have a SN7544?? h-bridge to control the direction. I have searched the forums to no avail and please be easy on me I am trying to figure this stuff out no knowing much about electronics whatsoever. If somebody has source code already that would be great.
From what I understand from your description should be pretty simple. You hook the pot up so you can read it's current value through one of the analog pins (analogRead), then if the value is < 512 drive the motor one way by settings the motor driver pins LOW/HIGH, and if the pot value is great than 512 then drive the motor pins HIGH/LOW.
I did exactly this (well almost) on the weekend with a Wii Nunchuck and a robot I was testing. Can post you some code this evening if you like.
what I wanted was to turn the knob and the motor will turn in that direction and the motor will stop if the knob is in the middle position. I actually just found a site that did exactly that with very minimal effort AND controlled the speed in which the motor turned according to the feedback from the potentiometer.
The code example on that page has a hard cutover from forward to backward - to stop the motor you'd want to leave some room in that middle section - eg 120-140 is stop, and you'd send LOW/LOW to the motor pins, or just LOW to enable.
Trying to do something similar with a joystick and hacking the AFMotor example Motor Test
I'm sure there are better ways of doing this but having some syntax issues with String substitution and getting this error
MotorTest_2.cpp: In function 'void loop()':
MotorTest_2:42: error: no matching function for call to 'AF_DCMotor::run(String&)'
C:\Documents and Settings\handyman\My Documents\downloads\arduino-0021\arduino-0021\libraries\AFMotor/AFMotor.h:84: note: candidates are: void AF_DCMotor::run(uint8_t)
I'm trying to set dir variable to RELEASE, FORWARD, or BACKWARD based on xa which is 0 at center position and -127 to +127 at the end positions.
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
#include <AFMotor.h>
#include <math.h>
AF_DCMotor motor(4);
//AF_DCMotor motor(4);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
// turn on motor
motor.setSpeed(200);
motor.run(RELEASE);
}
void loop() {
int x = analogRead(3);
int y = analogRead(5);
int xa = ((x/4)-128); // gives you range of -127 to +127 and 0 at center stick
int ya = ((y/4)-128);
int pos1 = (128 + xa); // gives you range of 0 to 256 with 128 at center stick
int pos2 = (128 + ya);
String dir = "RELEASE";
if (xa >= 5) {
String dir = "FORWARD";
}
if (xa <= -5) {
String dir = "BACKWARD";
int fabs ( xa );
}
motor.run(dir);
motor.setSpeed(xa);
delay(10);
uint8_t i;
Serial.println("tick");
Serial.println(pos1);
Serial.println(pos2);
Serial.println(xa);
Serial.println(ya);
delay(500);
}
I'm more hacker than programmer and new to Arduino as well. That code doesnt seem to work either. It does compile but not setting dir to anything that works.
You could, but the range of values that are possible (FORWARD, RELEASE, BRAKE, and BACKWARD) fits in a byte, and a byte is what motor.run() expects as input.
Using a larger type than needed is a waste of memory.
Also, if the Arduino team ever wises up and decides that turning off all compiler warnings is not a good thing to do, the compiler WILL tell you all about the type mis-matches that are occurring.
Even though you don't hear about them, the compiler has extra work to do every time there is a type mis-match.