Start line follower using board button (I NEED HELP)


#1

Helo, I have made a software using arduino for my mBot that follows a white line. I want to start it using the board button, but I don’t know how to write it in arduino, so I need your help. Thank you.


#2

Something like:

mBot Program
  wait until <button pressed>
     (rest of your program)

#3

I think that I need some libraries to put that instruction into Arduino, I have already instaled MeMCore.h but it doesn’t work. Thank you anyways.


#4

Here is an instruction on the learn website to guide adding Makeblock library to Arduino software.


#5

I have already done it guys. The board button was set as A7 digital pin, so I made a void to initialize It using “if”, “else” and digitalRead.


#6

I have done the same thing using mblock (mine goes on black line) and i have the following instructions:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

#include <MeMCore.h>

MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
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;
}
motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
}
double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;
MeLineFollower linefollower_3(3);

void setup(){
pinMode(A7,INPUT);
while(!((0^(analogRead(A7)>10?0:1))))
{
_loop();
}
if(((linefollower_3.readSensors())==(0))){
move(1,100);
}else{
if(((linefollower_3.readSensors())==(1))){
move(3,100);
}else{
if(((linefollower_3.readSensors())==(2))){
move(4,100);
}else{
move(2,100);
}
}
}
}

void loop(){
_loop();
}

void _delay(float seconds){
long endTime = millis() + seconds * 1000;
while(millis() < endTime)_loop();
}

void _loop(){
}

i don’ t know if i helped, i am trying to make it start when button pressed and also stop when button pressed for the second time…no luck yet!


#7

To make the button start and stop the mBot, you’ll need to keep a variable, called a state variable that contains the current state of the mBot (moving / not moving) and you will need to check that every time you go through your loop. The example below plays a tone but uses the onboard button to control the state of the program. I left in the debugging Serial.println statement so that you can see the current value of the state variable in the serial monitor.

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

#include <MeMCore.h>

#define NOT_RUNNING 0
#define RUNNING     1

int state;
MeBuzzer buzzer;

void setup(){
  state = NOT_RUNNING;
  Serial.begin(9600);
  pinMode(A7,INPUT);

  // wait until onboard button pressed
  while (state == NOT_RUNNING) {
    checkButtonPress();
    }

  while (state == RUNNING) {
    Serial.println("State: " + String(state));
    playTone();
    checkButtonPress();
  }
}

void loop() {
}

void playTone() {
  if (state == RUNNING) {
    // play the tone
    buzzer.tone(392, 500);
    delay(20);
  } else {
    // turn off the tone
    buzzer.tone(0, 500);
  }
}

void checkButtonPress() {
  if (analogRead(A7) == 0) {
    // onboard button has been pressed, toggle state
    state = !state;
  }
}

#8

Thank you all guys


#9