I want to build a simple car which moves forward using the x-axis readings of an accelerometer.
The arduino board is attached to a motor shield and the motor shield is attached to the accelerometer. The x pin of the acceleromter is attached to analog pin 3 of the arduino board.
This is my code int var1; void setup() {
pinMode(A3,INPUT);*
pinMode(2,OUTPUT);*
pinMode(3,OUTPUT);*
pinMode(4,OUTPUT);*
pinMode(5,OUTPUT);*
Serial.begin(9600);* } void loop() {
var1=analogRead(A3);*
Serial.println(var1);*
delay(800);*
if(var1<300)*
{*
digitalWrite(2,HIGH);*
digitalWrite(3,LOW);*
digitalWrite(4,HIGH);*
digitalWrite(5,LOW);*
}* }
It isn't working though. Please help. My connections are double checked. So i don't think there's any fault over there
We would like to know more.
Can you give a link to the motor shield you are using ( just copy the url in the text ).
Which accelerometer are you using ? Which Arduino board ?
What did you see on the serial monitor ? Did those values confirm that the accelerometer is working ?
Can you make the car move without the accelerometer ?
The analog input pins default to inputs - you don't need to set pin mode explicitly.
I have no idea what sort of accelerometer you're using or what range of input values it produces on pin A3. You've got some print statements there - do they show you the sort of values you expect, and in particular are the values going over 300 in the situation where you want the motor to run?
You probably want some code to stop the motor when the value goes under 300.
Do those four digitalWrite() statements actually make the motor run? If you haven't tested that yet, test it by writing a sketch that just runs the motor.