Problem Programming AnalogWrite from Accelerometer to DC Motors

Hey guys, I'm quite new to this arduino but my projects calls for me to use the data received from the accelerometer and make an analogWrite for the two DC motors i've wired onto my arduino. This is the program I tried to use to make my motor move according to the analog data that receive fro the accelerometer, but after uploading the program, the DCmotor makes a small beep sound. Is there something wrong with my wiring, programming? or maybe both? In addition to that, i've tried running a simple digitalWrite on pin5 but my motor doesn't run. Checked the motor by placing it into the 5 volt, and it works just fine. Once again, what am I doing wrong? Thanks.

#Code Start
// these constants describe the pins. They won't change:

int xpin = A0; // x-axis of the accelerometer
int ypin = A1; // y-axis
int zpin = A2; // z-axis (only on 3-axis models)

const int motor = 5; // sets digital pin 5 as motor

int Xpindata = 0; /used as a store variable

void setup()
{
// initialize the serial communications:
Serial.begin(9600);
pinMode(motor, OUTPUT);

}

void loop()
{
int val = analogRead(xpin);

val = constrain(val, 329,400);
int vallevel = map(val,329,400,255,0);

analogWrite(motor,vallevel);

// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin)- 323);
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin)-277);
Serial.println();
// delay before next reading:
delay(500);

}
#Code End

  int val = analogRead(xpin);
 
  val = constrain(val, 329,400);
 int vallevel = map(val,329,400,255,0);
 
  analogWrite(motor,vallevel);

  // print the sensor values:
  Serial.print(analogRead(xpin));

Read the pin. Do something with the data read from the pin. Then, read and print something else. What have you learned? Not a thing.

Well how about thinking about the problem for a few moments?

Do you know whether the problem is that you are not getting suitable data from the accelerometer?
OR is the problem that you can't control the motor?
OR is it something else?

Write two separate test programs. The first one collects the data from the accelerometer and displays the values on your PC. The second makes the motor run at different speeds. When both of them work separately move on to creating a single program that combines what you have learned.

...R

Are you trying to drive the motors directly from the arduino? you'll damage your arduino. you should get a motor driver with PWM. then your code should look something like this

// these constants describe the pins. They won't change:

int xpin = A0;                  // x-axis of the accelerometer
int ypin = A1;                  // y-axis
int zpin = A2;                  // z-axis (only on 3-axis models)

const int motorpin1 = 5;
const int motorpin2 = 6;
const int motorSpeed = 3;

int Xpindata = 0;    /used as a store variable

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
  pinMode(motorSpeed, OUTPUT);
  
  }

void loop()
{
  int val = analogRead(xpin);
  
  val = constrain(val, 329,400);
 int vallevel = map(val,329,400,255,0);
  
  digitalWrite(motorpin1, HIGH);
  digitalWrite(motorpin2, LOW);
  analogWrite(motorSpeed,vallevel);

  // print the sensor values:
  Serial.print(analogRead(xpin));
  // print a tab between values:
  Serial.print("\t");
  Serial.print(analogRead(ypin)- 323);
  // print a tab between values:
  Serial.print("\t");
  Serial.print(analogRead(zpin)-277);
  Serial.println();
  // delay before next reading:
  delay(500);

  }

Thank You for replying, I have been able to collect data, but now i'm trying to configure the data to set the voltage that is going to the motor. Will that still damage the arduino? The plan was to have the arduino programmed to control a small gokart. Presently, i'm prototyping the program with the DC motor. Is there anyway I can go about doing this? The problem right now is that i can't seem to control the motor with the data that I can collect. What should I do?

What should I do?

Substitute an LED with current limiting resistor. Actually making a motor move is not critical to understanding that the accelerometer is not giving you anything like angle data.

You'll need a motor driver with pwm, like I mentioned earlier. For something chep and easy, I like this one.

The code I put earlier will work with it.

I'm using it in my rc car.
http://arduino.cc/forum/index.php/topic,154463.0.html
It also use an accelerometer with similar code.

Could i program a microcontroller to do the PWM side of it? Also, attached to the post is a picture of the micro controller and my setup. Its on an arduino Duemilanove, ad the accelerometer model is an ADXL335. Once again, I apologize for my lack of understanding about the subject. I've been trying to get this down in the past 2 days and my head is jumbled up right now. Sorry .

Could i program a microcontroller to do the PWM side of it?

Sure. The PWM side of what?

What's the chip in second pic?

A post before this one, cwhummel mentioned that I would need a PWM driver so it wouldn't spoil the arduino and it would work with the program( did it get that right? ). Could I program that side on the other chip that I posted a picture of with?

Additionally, Here is a video of what I've manage to do so far, but it hasn't been consistent. I'll have to upload the program again, or tinker with the wires a little bit for it to happen, other times it just does nothing but emit a soft beep. It sounds like its coming from the motor. So what seems to be the problem? Once again, Thanks so much for the help so far.

Prototype Demo.mp4 (800 KB)

Could I program that side on the other chip that I posted a picture of with?

Again:

What's the chip in the second pic?

I'm not too sure what that chip really is, but its model is a ULN2003AN, but looking it up online, Texas instrument says its a high voltage, high current, darlington transitor array. I've seen youtube videos about it a couple of days back, saying that it could be programmed to run minor tasks.

Also, here is a youtube video of it working, the previous attachment had problems. Sorry about that. - YouTube
I hope this link works. By the way cwhummel, that RC car is pretty cool, I hope I reach that level to do that some day.

Thanks! If it doesn't rain this afternoon I hope to get a new video showing off the accelleromter functions now that I'm all done with the controller.

That chip looks like its good for a stepper motor, not too sure about a dc motor. I would still recommend a motor driver. You'll need one in the future anyway if you want to make a go cart.

Thank you very much, I suppose I'm making a stepper motor then?

Also, I failed to mention this in the earlier posts but the go kart had already been made. I'm just programming the arduino to control the gokart. Its going to be manual driven , so I wouldn't need to make it wireless. But thats basically what i'm trying to program- the gokart's control system. Think of it as a single steering wheel. That would be the best description.

Finally, would you happen to know why the program is inconsistent? The video i uploaded was among the successful trials. At times it'll just stay still, emitting a small beep from the motor. Am I damaging the motor by doing that?

Good luck with those video's i'll be sure to check them up for future references.

The wireless part doesn't matter.

You're not making a stepper motor, the chip that you have is made to drive one (this is not a path you want to take).

In order to use the motor that you do have, you need a motor driver, as stated earlier. This will allow for an external power source to power the motor without drawing too much current from (and damaging) the arduino. It will take two digital pins (one high, one low) to tell the driver to go either forward or reverse, and one pwm pin to tell the driver what speed you want.

Thank you very much, I've learned alot from you guys in this short period of time.