Hello,
Sorry in advance like i say. idk what is going on here hoping one of you pro's might get it. Ill post the code under. Would seem to work. My arduino has two servos that are wired direct to the motors, running of a l9110 dual h-bridge. it should back up when the ultrasonic sensor reads <4. It does that part fine. When I try to use the IR remote i am using to control it. it shorts. i pull the power really fast and its not fried but something is wrong with the code and not the wiring is what im fairly certain of.
code:
#include "IRremote.h"
int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
int trigPin = 13;
int echoPin = 12;
int MOTOR_1A = 2;
int MOTOR_1B = 3;
int MOTOR_2A = 4;
int MOTOR_2B = 5;
int LED6 = 6;
int LED10 = 10;
int LED11 = 11;
long duration; //long duration = 0;
int distance; //int distance = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Serial communication working");
irrecv.enableIRIn();
delay(1000);
Serial.println("IR enabled");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(MOTOR_1A, OUTPUT);
pinMode(MOTOR_1B, OUTPUT);
pinMode(MOTOR_2A, OUTPUT);
pinMode(MOTOR_2B, OUTPUT);
pinMode(LED6, OUTPUT);
digitalWrite(LED6, HIGH);
delay(1000);
digitalWrite(LED6,LOW);
delay(20);
digitalWrite(LED10, HIGH);
delay(1000);
digitalWrite(LED10,LOW);
delay(20);
digitalWrite(LED11, HIGH);
delay(1000);
digitalWrite(LED11,LOW);
delay(20);
}
void loop()
{
if(irrecv.decode(&results))
{
Serial.println(results.value);
processButton();
irrecv.resume();
}
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
}
if(distance <= 4)
{
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_2B, LOW);
digitalWrite(LED6, HIGH);
digitalWrite(LED10, LOW);
}
else if (distance > 4)
{
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED10, HIGH);
}
}
void processButton()
{
switch (results.value)
{
//**********************FORWARD ARROW_A
case 32:
// Serial.println("1");
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, HIGH);
break;
//**********************FORWARD ARROW_B
case 2080:
// Serial.println("1");
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, LOW);
break;
//**********************BACK ARROW_A
case 33:
// Serial.println("2");
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_2B, LOW);
break;
//**********************BACK ARROW_B
case 2081:
// Serial.println("2");
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, LOW);
break;
//**********************LEFT ARROW_A
case 17:
// Serial.println("3");
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, HIGH);
break;
//**********************LEFT ARROW_B
case 2065:
// Serial.println("3");
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, LOW);
break;
//**********************RIGHT ARROW_A
case 16:
// Serial.println("4");
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_2B, LOW);
break;
//**********************RIGHT ARROW_B
case 2064:
// Serial.println("4");
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_2B, LOW);
break;
}
}
I hope its not too much trouble I put great effort into trying not to ask for help.