hello, I'm trying to merge two codes together for my project I normally can do this with no problem but I cant figure out a way to do it and I was wondering if you guys can help. both codes work by themselves. I'm trying to control a servo with a ir and two dc motors with ir I'm thinking it has something to do with the bool. the first code is for dc motors second code is for servo and the third code is me trying to merge them together please help! ive been stuck on this for days and cant go forward with my project till this is done.
here is the dc motor code...
#include <IRremote.h>
int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =65;//define the spead of motor
void setup()
{
irrecv.enableIRIn();
pinMode(pinI1,OUTPUT);// UP DOWN MOTOR
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);// FORWARD BACKWARD MOTOR
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
irrecv.resume();
if (results.value == 0x827D40BF){ // forward
//SPEED
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn FORWARD, BACKWARD MOTOR
digitalWrite(pinI3,LOW);
}else if(results.value == 0x827DC03F){ // UP
analogWrite(speedpinA,spead);//SPEED
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);
}
else if(results.value == 0x827D807F){ // DOWN
analogWrite(speedpinA,spead);//SPEED
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
else if(results.value == 0x827D10EF){ // backward
analogWrite(speedpinB,spead);//SPEED
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
}
else if(results.value == 0x827DA05F){ // type your remote stop robot control button hex value in underlined area.
digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
digitalWrite(speedpinB,LOW);
}
}
}
and here is the servo code...
#include <IRremote.h> //must copy IRremote library to arduino libraries
#include <Servo.h>
#define plus 0x827D20DF //clockwise rotation button
#define minus 0x827D609F //counter clockwise rotation button
int RECV_PIN = 4; //IR receiver pin
Servo servo;
int val; //rotation angle
bool cwRotation, ccwRotation; //the states of rotation
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
servo.attach(5); //servo pin
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
if (results.value == plus)
{
cwRotation = !cwRotation; //toggle the rotation value
ccwRotation = false; //no rotation in this direction
}
if (results.value == minus)
{
ccwRotation = !ccwRotation; //toggle the rotation value
cwRotation = false; //no rotation in this direction
}
}
if (cwRotation && (val != 175)) {
val++; //for colockwise button
}
if (ccwRotation && (val != 0)) {
val--; //for counter colockwise button
}
servo.write(val);
delay(200); //General speed
}
and this is me trying to merge the two codes together it does verify that their are no errors...
#include <IRremote.h>
#include <Servo.h>
#define plus 0x827D20DF //clockwise rotation button
#define minus 0x827D609F //counter clockwise rotation button
int RECV_PIN = 4;
Servo servo;
int val; //rotation angle
bool cwRotation, ccwRotation; //the states of rotation
IRrecv irrecv(RECV_PIN);
decode_results results;
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =65;//define the spead of motor
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(pinI1,OUTPUT);// UP DOWN MOTOR
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);// FORWARD BACKWARD MOTOR
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
servo.attach(5); //servo pin
}
void loop() {
if (irrecv.decode(&results)) {
irrecv.resume();
if (results.value == 0x827D40BF){ // forward
//SPEED
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn FORWARD, BACKWARD MOTOR
digitalWrite(pinI3,LOW);
}else if(results.value == 0x827DC03F){ // UP
analogWrite(speedpinA,spead);//SPEED
digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
digitalWrite(pinI1,HIGH);
}
else if(results.value == 0x827D807F){ // DOWN
analogWrite(speedpinA,spead);//SPEED
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
}
else if(results.value == 0x827D10EF){ // backward
analogWrite(speedpinB,spead);//SPEED
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
}
else if(results.value == 0x827DA05F){ // type your remote stop robot control button hex value in underlined area.
digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
digitalWrite(speedpinB,LOW);
}
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
if (results.value == plus)
{
cwRotation = !cwRotation; //toggle the rotation value
ccwRotation = false; //no rotation in this direction
}
if (results.value == minus)
{
ccwRotation = !ccwRotation; //toggle the rotation value
cwRotation = false; //no rotation in this direction
}
}
if (cwRotation && (val != 175)) {
val++; //for colockwise button
}
if (ccwRotation && (val != 0)) {
val--; //for counter colockwise button
}
servo.write(val);
delay(200); //General speed
}}}