Hello Arduino lovers,
I have built a circuit where I am getting values from a pre-amplified electrec mic and using those values to turn my gearhead motor in two directions. I am using a h-bridge to reverse the direction. It is working ok, only my problem is it triggers kind of with delay of 1 second to 2 second than it should be. I am assuming this could be because of the serial packets that I am sending. Here is my code and I really welcome your advices about the code.
int analogPin = 2; // analogIn
int analogValue = 0; // keep the value here
#define motor1Pin 13
#define motor2Pin 12
#define speedPin 9 // this is the pin that PVM from H-Bridge
void setup() {
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(speedPin, OUTPUT);
digitalWrite(speedPin, HIGH);
Serial.begin(9600);
}
void loop() {
analogValue = analogRead(analogPin)/4; // scale down a little bit.
if(analogValue > 200) { // if iserial value is over 200
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}
else if (analogValue <200) { // reverse
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor1Pin, LOW);
}
//print in many formats:
// print a tab
Serial.print(analogValue); // print the ASCII encoded decimal analogValue
Serial.print('\t'); // print a tab
Serial.println(); // print a linefeed and carriage return
delay(10);
}