I am trying to adjust the right motor speed and by an increment counter (search), as below:
in the setup function:
void setup(){
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
search = 30;
while(!(search > 50)){
seg7_6.display(float(search));
// changer the motor power //
Encoder_1.setTarPWM(-1search/100.0255);
Encoder_2.setTarPWM(80/100.0*255);
search += 0.005;
delay (1);
}
}
I increased the right motor power within the while loop by the variable “search”. Therefore the ranger would circling with a growing radius.
Yet in reality, the ranger action is:
1.none of the motor is moving, and wait until the “search” counts to the max, 50,
2.then both of the motors start move as the left one 80% and the right one 50% power.
What happened to the ranger.
ps : an mBot may perform the same action correctly, yet the ranger acts strange.
below is the whole code:
#include <Arduino.h>;
#include <Wire.h>;
#include <SoftwareSerial.h>;
#include <MeAuriga.h>;
double distance;
double obj_dist;
double search;
Me7SegmentDisplay seg7_6(6);
MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);
void isr_process_encoder1(void)
{
if(digitalRead(Encoder_1.getPortB()) == 0){
Encoder_1.pulsePosMinus();
}else{
Encoder_1.pulsePosPlus();
}
}
void isr_process_encoder2(void)
{
if(digitalRead(Encoder_2.getPortB()) == 0){
Encoder_2.pulsePosMinus();
}else{
Encoder_2.pulsePosPlus();
}
}
void move(int direction, int speed)
{
int leftSpeed = 0;
int rightSpeed = 0;
if(direction == 1){
leftSpeed = -speed;
rightSpeed = speed;
}else if(direction == 2){
leftSpeed = speed;
rightSpeed = -speed;
}else if(direction == 3){
leftSpeed = -speed;
rightSpeed = -speed;
}else if(direction == 4){
leftSpeed = speed;
rightSpeed = speed;
}
Encoder_1.setTarPWM(leftSpeed);
Encoder_2.setTarPWM(rightSpeed);
}
void _loop(){
Encoder_1.loop();
Encoder_2.loop();
}
void _delay(float seconds){
long endTime = millis() + seconds * 1000;
while(millis() < endTime)_loop();
}
void setup(){
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
search = 40;
while(!(search > 50)){
seg7_6.display(float(search));
Encoder_1.setTarPWM(-1search/100.0255);
Encoder_2.setTarPWM(80/100.0*255);
search += 0.005;
delay (1);
}
}
void loop(){
_loop();
}