How do I combine the following two sets of code? Can anyone send me a copy of code that is similar to this, i.e., code that will make the DF 4WD robot move around as well as respond to the encoders?
I have successfully used the following code to run my DF Robot 4WD platform through a series of movements:
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control HIGH = reverse left side
// LOW = forward left side
int M2 = 7; //M2 Direction Control HIGH = forward right side
// LOW = reverse right side
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void setup(void)
{
pinMode(5, OUTPUT);
Serial.begin(19200); //Set Baud Rate
}
void loop(void)
{
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(10000);
stop();
delay(3000);
analogWrite (E1,190); // slight right turn
digitalWrite(M1,LOW);
analogWrite(E2, 70);
digitalWrite(M2,HIGH);
delay(2800);
stop();
delay(3000);
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(12000);
stop();
delay(4000);
}
I have since added encoders to the platform and would like to use the following code to control these encoders:
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 4;//B pin -> the digital pin 4
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction
void setup()
{
Serial.begin(57600);//Initialize the serial port
EncoderInit();//Initialize the module
}
void loop()
{
Serial.print("Pulse:");
Serial.println(duration);
duration = 0;
delay(500);
}
void EncoderInit()
{
Direction = true;//default -> Forward
pinMode(encoder0pinB,INPUT);
attachInterrupt(0, wheelSpeed, CHANGE);
}
void wheelSpeed()
{
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
{
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
{
Direction = false; //Reverse
}
else if(val == HIGH && !Direction)
{
Direction = true; //Forward
}
}
encoder0PinALast = Lstate;
if(!Direction) duration++;
else duration--;
}
As I said above...My question is how do I combine these two sets of code? Can anyone send me a copy of code that is similar to this, i.e., code that will make the DF 4WD robot move around as well as respond to the encoders? When I upload the encoder code by itself, nothing happens to the robot. I assume I need to somehow mesh these two sets of code together, but I don't know how.
I have successfully used the following code to run my DF Robot 4WD platform through a series of movements:
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control HIGH = reverse left side
// LOW = forward left side
int M2 = 7; //M2 Direction Control HIGH = forward right side
// LOW = reverse right side
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void setup(void)
{
pinMode(5, OUTPUT);
Serial.begin(19200); //Set Baud Rate
}
void loop(void)
{
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(10000);
stop();
delay(3000);
analogWrite (E1,190); // slight right turn
digitalWrite(M1,LOW);
analogWrite(E2, 70);
digitalWrite(M2,HIGH);
delay(2800);
stop();
delay(3000);
analogWrite (E1,190); // Go Forward
digitalWrite(M1,LOW);
analogWrite (E2,120);
digitalWrite(M2,HIGH);
delay(12000);
stop();
delay(4000);
}
I have since added encoders to the platform and would like to use the following code to control these encoders:
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 4;//B pin -> the digital pin 4
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction
void setup()
{
Serial.begin(57600);//Initialize the serial port
EncoderInit();//Initialize the module
}
void loop()
{
Serial.print("Pulse:");
Serial.println(duration);
duration = 0;
delay(500);
}
void EncoderInit()
{
Direction = true;//default -> Forward
pinMode(encoder0pinB,INPUT);
attachInterrupt(0, wheelSpeed, CHANGE);
}
void wheelSpeed()
{
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
{
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
{
Direction = false; //Reverse
}
else if(val == HIGH && !Direction)
{
Direction = true; //Forward
}
}
encoder0PinALast = Lstate;
if(!Direction) duration++;
else duration--;
}
As I said above...My question is how do I combine these two sets of code? Can anyone send me a copy of code that is similar to this, i.e., code that will make the DF 4WD robot move around as well as respond to the encoders? When I upload the encoder code by itself, nothing happens to the robot. I assume I need to somehow mesh these two sets of code together, but I don't know how.




Logged

