Need help making arduino robot start and stop when button is pressed in MIT app inventor

Hi, I am working on a project where my arduino robot travels around an area in a predefined path. My code works fine, but I just need it to start and stop whenever someone presses a button in the MIT app inventor. So, when it's turned on, I want it to stay in place until someone presses the start button and then stop when the stop button is pressed. Thank you.

// Parameters for the sets of code
const int num_sets = 4;  // Number of sets of parameters

// Define parameters for each set
const int drive_distances[num_sets] = {60, 30, 60, 30};        // cm
const int motor_powers_left[num_sets] = {50, 0, 70, 50};       // 0-255
const int motor_powers_right[num_sets] = {60, 50, 70, 0};     // 0-255
const int motor_offset = 0;                             // Diff. when driving straight
const int wheel_d = 70;                                  // Wheel diameter (mm)
const float wheel_c = PI * wheel_d;                      // Wheel circumference (mm)
const int counts_per_rev = 384;                          // (4 pairs N-S) * (48:1 gearbox) * (2 falling/rising edges) = 384

// Pins
const int enc_r_pin = 3;  // Motor A
const int enc_l_pin = 2;  // Motor B
const int enA = 11;
const int in1 = 13;
const int in2 = 12;
const int enB = 10;
const int in3 = 9;
const int in4 = 8;

// Globals
volatile unsigned long enc_l = 0;
volatile unsigned long enc_r = 0;

void setup() {
  // Debug
  Serial.begin(9600);

  // Set up pins
  pinMode(enc_l_pin, INPUT_PULLUP);
  pinMode(enc_r_pin, INPUT_PULLUP);
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Set up interrupts
  attachInterrupt(digitalPinToInterrupt(enc_l_pin), countLeft, CHANGE);
  attachInterrupt(digitalPinToInterrupt(enc_r_pin), countRight, CHANGE);

  // Run the code for each set of parameters
  for (int i = 0; i < num_sets; i++) {
    // Drive straight for the current set of parameters
    delay(1000);
    driveStraight(drive_distances[i], motor_powers_right[i], motor_powers_left[i]);
  }
}

void loop() {
  // Do nothing
}

void driveStraight(float dist, int power_left, int power_right) {
  unsigned long num_ticks_l;
  unsigned long num_ticks_r;

  // Used to determine which way to turn to adjust
  unsigned long diff_l;
  unsigned long diff_r;

  // Reset encoder counts
  enc_l = 0;
  enc_r = 0;

  // Remember previous encoder counts
  unsigned long enc_l_prev = enc_l;
  unsigned long enc_r_prev = enc_r;

  // Calculate target number of ticks
  float num_rev = (dist * 10) / wheel_c;  // Convert to mm
  unsigned long target_count = num_rev * counts_per_rev;

  // Debug
  Serial.print("Driving for ");
  Serial.print(dist);
  Serial.print(" cm (");
  Serial.print(target_count);
  Serial.print(" ticks) at ");
  Serial.print(power_left);
  Serial.print(", ");
  Serial.print(power_right);
  Serial.println(" motor power");

  // Drive until one of the encoders reaches desired count
  while ((enc_l < target_count) && (enc_r < target_count)) {
    // Sample number of encoder ticks
    num_ticks_l = enc_l;
    num_ticks_r = enc_r;

    // Print out current number of ticks
    Serial.print(num_ticks_l);
    Serial.print("\t");
    Serial.println(num_ticks_r);

    // Drive
    drive(power_left, power_right);

    // Number of ticks counted since last time
    diff_l = num_ticks_l - enc_l_prev;
    diff_r = num_ticks_r - enc_r_prev;

    // Store current tick counter for next time
    enc_l_prev = num_ticks_l;
    enc_r_prev = num_ticks_r;

    // If left is faster, slow it down and speed up right
    if (diff_l > diff_r) {
      power_left -= motor_offset;
      power_right += motor_offset;
    }

    // If right is faster, slow it down and speed up left
    if (diff_l < diff_r) {
      power_left += motor_offset;
      power_right -= motor_offset;
    }

    // Brief pause to let motors respond
    delay(20);
  }

  // Brake
  brake();
}

void drive(int power_a, int power_b) {
  // Constrain power to between -255 and 255
  power_a = constrain(power_a, -255, 255);
  power_b = constrain(power_b, -255, 255);

  // Left motor direction
  if (power_a < 0) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
  } else {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  }

  // Right motor direction
  if (power_b < 0) {
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
  } else {
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
  }

  // Set speed
  analogWrite(enA, abs(power_a));
  analogWrite(enB, abs(power_b));
}

void brake() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

void countLeft() {
  enc_l++;
}

void countRight() {
  enc_r++;
}

First, you will need to wire your button... starting at the Arduino pin (for example, 2), connect a wire to a button leg, then on a normally-open button leg, connect a wire to ground. When you press the button, the wires will be shorted.

In your code, define a button, for example:

byte buttonPin = 2; // start/stop button pin

Then inside your setup() you will need to configure the pin

pinMode(buttonPin, INPUT_PULLUP);

At this point, your button will be HIGH when NOT pressed and LOW when PRESSED.

Next, create a "debounced" button reading function... in which you will keep track of "odd" number of presses versus "even" number of presses, turning the project ON with odd and OFF with EVEN... because your Arduino is never turned OFF, your "OFF" state is doing nothing other than waiting for the next (odd) button press.

Finally, in "loop()" call this button reading function...

What kind of technology are you using to communicate between the MIT-App and your arduino?
Is it by wire?
is it wireless?

if it is wireless
is it bluetooth?
is it WiFi?

Hi, I'm connecting it wirelessly with a HC wireless Bluetooth module.

programming is very different from snap-chattering.
You have to describe things very detailed and very precisely. Otherwise it just won't work

So what exact type of bluetooth-module?
what exact type of microcontroller?

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

I'm using this Arduino UNO R3 (https://www.amazon.com/Arduino-A000066-ARDUINO-UNO-R3/dp/B008GRTSV6) and HC-06 RS232 4 Pin Wireless Bluetooth Serial RF Transceiver Module (https://www.amazon.com/dp/B07VL6ZH67?ref=ppx_yo2ov_dt_b_product_details&th=1).

the less details about your whole project you are writing the more work I will keep on your shoulders
https://www.google.com/search?as_q=arduino+HC06+tutorial

Man, I just want to know how to modify my code.

I found this link helpful when getting started with MIT app inventor

Code-Modification:

  • add library software-serial

  • open software-serial

    • according to the baudrate of your bluetooth-modul
    • according to the IO-pins you want to use for the serial connection
  • read in the character-sequence according to the character-sequence that is defined in and send out from your MIT-APP

As you have not specified these details the answers stays pretty generalised which of course does not really help.

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