How to use moveTo command multiple times


#1

Hello, I am trying to use a function, I created which moves my mbot forward then stops, but the function only runs once, how can I make the function run until an condition is met.

Function:
int moveDirection(char Direction){
switch(Direction)
{
case ‘0’: // forward
Encoder_1.moveTo(-850);
Encoder_2.moveTo(850);
break;
case ‘1’: //left
Encoder_1.moveTo(-520);
Encoder_2.moveTo(-520);
break;
case ‘2’: // right
Encoder_1.moveTo(520);
Encoder_2.moveTo(520);
break;
case ‘3’: //backwards
Encoder_1.moveTo(850);
Encoder_2.moveTo(-850);
break;
case ‘4’: // 180
Encoder_1.moveTo(-1000);
Encoder_2.moveTo(-1000);
break;
}
Encoder_1.loop();
Encoder_2.loop();
}

Code:
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include “MeAuriga.h”

//Encoder Motor Delcare
MeEncoderOnBoard Encoder_1(SLOT1); // Declare encoder motors
MeEncoderOnBoard Encoder_2(SLOT2);

//Motor Declare
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 setup(){
//PWM Motor Setup
Serial.begin(9600);
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
//Set Pwm 8KHz
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
Encoder_1.setPulse(9);
Encoder_2.setPulse(9);
Encoder_1.setRatio(39.267);
Encoder_2.setRatio(39.267);
Encoder_1.setPosPid(1.8,0,1.2);
Encoder_2.setPosPid(1.8,0,1.2);
Encoder_1.setSpeedPid(0.18,0,0);
Encoder_2.setSpeedPid(0.18,0,0);
}
void loop(){
moveDirection(‘0’);
if (frontUltrasonicSensor.distanceCm() <= 7){
turnAbsolute(90, true, 100);
delay(500);
turnAbsolute(90, true, 100);
}
}


#2

hi few thing may help you

at the and of the void loop before the last"}" you need to add:
Encoder_1.loop();
Encoder_2.loop();
(look in the makeblock library encoderonboard meauriga)

the base of my void move is like that

void move(int direction, int moveSpeed) {
if (direction == 8) {//forward
moteurG.setTarPWM(moveSpeed);
moteurD.setTarPWM(-moveSpeed);
} else if (direction == 7) {//foward left
moteurG.setTarPWM(moveSpeed * 2.5);
moteurD.setTarPWM(-moveSpeed / 2);
} else if (direction == 9) {//forward right
moteurG.setTarPWM(moveSpeed / 2);
moteurD.setTarPWM(-moveSpeed * 2.5);
} else if (direction == 2) {//reverse
moteurG.setTarPWM(-moveSpeed);
moteurD.setTarPWM(moveSpeed);
} else if (direction == 1) {//back left
moteurG.setTarPWM(-moveSpeed / 2);
moteurD.setTarPWM(moveSpeed * 2.5);
} else if (direction == 3) {//Back right
moteurG.setTarPWM(-moveSpeed * 2.5);
moteurD.setTarPWM(moveSpeed / 2);
} else if (direction ==4) {//left
moteurG.setTarPWM(-moveSpeed);
moteurD.setTarPWM(-moveSpeed);
} else if (direction ==6) {//right
moteurG.setTarPWM(moveSpeed);
moteurD.setTarPWM(moveSpeed);
}else if (direction == 5) {//stop
moteurG.setTarPWM(0);
moteurD.setTarPWM(0);
}
}

after I don t understand why you don t you use moveDirection to turn instead of turnAbsolute(90, true, 100);
I hope that will help you


#3

@el_lolo

I have already added
Encoder_1.loop();
Encoder_2.loop();
its in the moveDirection function at the bottom. And the moveDirection is for driving forward a certain distance, while turnAbsolute can just turn a certain degree, but can’t drive forward a certain distance. Because the problem I am having is when i run the moveDirection function it works but if I call it a second time nothing happens.


#4

hi
Encoder_1.loop();
Encoder_2.loop();
I think you have to remove them from the function and add them at the end off the loop
can you join your script to see it pls


#5

I got rid of
Encoder_2.loop();
Encoder_2.loop();
and put them at the end of the loop,
void loop(){
moveDirection(‘0’);
moveDirection(‘0’);
Encoder_1.loop();
Encoder_2.loop();
}

but it only move forwards the same distance instead of moving forward double the distance, so it doesn’t matter how many moveDirection i add it moves the same distance of just one moveForward.


#6

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.