Assistance in diploma. Question programming the Arduino mega

Good afternoon. Guys help me with the code. Program:

#include <AFMotor.h> //Link library for working with shield

 // Plug motors to the terminal blocks M1, M2, M3, M4
 AF_DCMotor motor1(1);
 AF_DCMotor motor2(2);
 AF_DCMotor motor3(3);
 AF_DCMotor motor4(4);
 void setup(){
 // Set the maximum speed of the motor (similar work PWM)
 motor1.setSpeed(255);
 motor1.run(RELEASE);
 motor2.setSpeed(255);
 motor2.run(RELEASE);
 motor3.setSpeed(255);
 motor3.run(RELEASE);
 motor4.setSpeed(255);
 motor4.run(RELEASE);
 }

 void loop(){
 digitalRead(31); //reading from 31 Pin
 if (digitalRead(31) == HIGH) //condition if a high logic level
 {
//moving forward
 motor1.run(FORWARD); // Ask moving forward
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor2.setSpeed(100);
 motor3.setSpeed(100);
 motor4.setSpeed(100);
 delay(5000);

 // Stop motors
 

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(1000);
//moving backward
 motor1.run(BACKWARD); // Ask moving backward
 motor2.run(BACKWARD);
 motor3.run(BACKWARD);
 motor4.run(BACKWARD);
 motor1.setSpeed(100); // Set the speed
 motor2.setSpeed(100);
 motor3.setSpeed(100);
 motor4.setSpeed(100);
 delay(4000);

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(1000);

 // move left
 motor1.run(FORWARD); // Ask moving forward 2 motors
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor4.setSpeed(100);
 delay(1000);

 // Stop all motors
 /* Very not recommend sharply to switch the direction of rotation of the motor.
It is better to give a short period of time.*/

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(5000);
 }
 else
 {
// move left
 motor1.run(FORWARD); // Ask moving forward 2 motors
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor4.setSpeed(100);
 delay(1000);

 // Stop all motors
 /* Very not recommend sharply to switch the direction of rotation of the motor.
It is better to give a short period of time.*/

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(5000);
 }
 }

how can I make it to Arduino saw that if the pin three times will be a logic high level then the program will end because when I have the logic level low she turn left. And I need to make when it will be 3 times as high logic level motors stopped and not turned left to infinity.
P.S. Sorry immediately for my English I’m from Moldova

You can't if you uses delay(). Look at the blink without delay example.

Mark

how differently then need to do???

In the first post of this Thread I wrote an extended demo of the concept in the Blink Without Delay example sketch. It may give you some ideas.

It would be helpful if you could write your code comments in English - or at least in a language that uses the English alphabet.

…R

#include <AFMotor.h> //Link library for working with shield

 // Plug motors to the terminal blocks M1, M2, M3, M4
 AF_DCMotor motor1(1);
 AF_DCMotor motor2(2);
 AF_DCMotor motor3(3);
 AF_DCMotor motor4(4);
 void setup(){
 // Set the maximum speed of the motor (similar work PWM)
 motor1.setSpeed(255);
 motor1.run(RELEASE);
 motor2.setSpeed(255);
 motor2.run(RELEASE);
 motor3.setSpeed(255);
 motor3.run(RELEASE);
 motor4.setSpeed(255);
 motor4.run(RELEASE);
 }

 void loop(){
 digitalRead(31); //reading from 31 Pin
 if (digitalRead(31) == HIGH) //condition if a high logic level
 {
//moving forward
 motor1.run(FORWARD); // Ask moving forward
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor2.setSpeed(100);
 motor3.setSpeed(100);
 motor4.setSpeed(100);
 delay(5000);

 // Stop motors
 

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(1000);
//moving backward
 motor1.run(BACKWARD); // Ask moving backward
 motor2.run(BACKWARD);
 motor3.run(BACKWARD);
 motor4.run(BACKWARD);
 motor1.setSpeed(100); // Set the speed
 motor2.setSpeed(100);
 motor3.setSpeed(100);
 motor4.setSpeed(100);
 delay(4000);

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(1000);

 // move left
 motor1.run(FORWARD); // Ask moving forward 2 motors
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor4.setSpeed(100);
 delay(1000);

 // Stop all motors
 /* Very not recommend sharply to switch the direction of rotation of the motor.
It is better to give a short period of time.*/

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(5000);
 }
 else
 {
// move left
 motor1.run(FORWARD); // Ask moving forward 2 motors
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor4.setSpeed(100);
 delay(1000);

 // Stop all motors
 /* Very not recommend sharply to switch the direction of rotation of the motor.
It is better to give a short period of time.*/

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(5000);
 }
 }

Good afternoon. Guys help me with the code. Program:

#include <AFMotor.h> //Link library for working with shield

 // Plug motors to the terminal blocks M1, M2, M3, M4
 AF_DCMotor motor1(1);
 AF_DCMotor motor2(2);
 AF_DCMotor motor3(3);
 AF_DCMotor motor4(4);
 void setup(){
 // Set the maximum speed of the motor (similar work PWM)
 motor1.setSpeed(255);
 motor1.run(RELEASE);
 motor2.setSpeed(255);
 motor2.run(RELEASE);
 motor3.setSpeed(255);
 motor3.run(RELEASE);
 motor4.setSpeed(255);
 motor4.run(RELEASE);
 }

 void loop(){
 digitalRead(31); //reading from 31 Pin
 if (digitalRead(31) == HIGH) //condition if a high logic level
 {
//moving forward
 motor1.run(FORWARD); // Ask moving forward
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor2.setSpeed(100);
 motor3.setSpeed(100);
 motor4.setSpeed(100);
 delay(5000);

 // Stop motors
 

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(1000);
//moving backward
 motor1.run(BACKWARD); // Ask moving backward
 motor2.run(BACKWARD);
 motor3.run(BACKWARD);
 motor4.run(BACKWARD);
 motor1.setSpeed(100); // Set the speed
 motor2.setSpeed(100);
 motor3.setSpeed(100);
 motor4.setSpeed(100);
 delay(4000);

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(1000);

 // move left
 motor1.run(FORWARD); // Ask moving forward 2 motors
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor4.setSpeed(100);
 delay(1000);

 // Stop all motors
 /* Very not recommend sharply to switch the direction of rotation of the motor.
It is better to give a short period of time.*/

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(5000);
 }
 else
 {
// move left
 motor1.run(FORWARD); // Ask moving forward 2 motors
 motor4.run(FORWARD);
 motor1.setSpeed(100); // Set the speed
 motor4.setSpeed(100);
 delay(1000);

 // Stop all motors
 /* Very not recommend sharply to switch the direction of rotation of the motor.
It is better to give a short period of time.*/

 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 delay(5000);
 }
 }[code]

how can I make it in to Arduino saw that if the pin three times will be a logic high level then the program will end because when I have the logic level low she turn left. And I need to make when it will be 3 times as high logic level motors stopped and not turned left to infinity. [/code]

Use a variable to count the number of times it’s HIGH. If the variable is less than 3, do your turning, if 3 then don’t do turning. Something like this:

int turnCount =0;  //put this before setup()
void loop(){
 if (digitalRead(31) == HIGH && turnCount <3){ 
    turnCount++;  //increase the count
// your current turn code here
  }
//You will need to reset turnCount to 0 at some time 
//but you've not given any information on how and when you want this.
// probably you'll want to do it in the else part.
}

;topics merged.

Do not cross-post.

thank you very much Henry_Best. This is what I needed.

Use a variable to count the number of times it's HIGH.

No. The variable needs to count the number of times the pin BECOMES high.

OP: Look at the state change detection example, to see how to detect that a pin has BECOME high (or low), rather than that the pin IS high (or low). You want to take action when the pin [u]changes[/u] state, not when it [u]is in[/u] some state.

PaulS:

Use a variable to count the number of times it's HIGH.

No. The variable needs to count the number of times the pin BECOMES high.

In the ideal world, yes, but his code has so many long delays in it (a total of 17 seconds), it won't make the slightest difference, unless he leans on the button for more than that length of time.

so step one getting rid of the delays() and step two using the state change idea to increment the count variable. a bit of work to be done... ;)

This is result:

#include <AFMotor.h>  // ?????????? ?????????? ??? ?????? ? ?????? 
#include <Servo.h> 
#include <Arduino.h>
// ?????????? ?????? ? ?????????? M1, M2, M3, M4
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup(){
	// ?????? ???????????? ???????? ???????? ??????? (?????? ?????? PWM) 
	motor1.setSpeed(255);
	motor1.run(RELEASE);
	motor2.setSpeed(255);
	motor2.run(RELEASE);
	motor3.setSpeed(255);
	motor3.run(RELEASE);
	motor4.setSpeed(255);
	motor4.run(RELEASE);
        int turnCount=0;
        int turnCount1=0;
}
int turnCount1=0;
int turnCount=0;
void loop(){
  
            digitalRead(31);
  
  if (digitalRead(31) == HIGH && turnCount <5)
  { 
        turnCount++; 
        motor1.run(FORWARD); // ?????? ???????? ??????
	motor2.run(FORWARD);
	motor3.run(FORWARD);
	motor4.run(FORWARD);
        motor1.setSpeed(150); // ?????? ???????? ????????
	motor2.setSpeed(150); 
	motor3.setSpeed(150); 
	motor4.setSpeed(150); 
	delay(350);

         // ????????????? ?????????
	/* ????? ?? ??????????? ????? ??????????? ??????????? ???????? ??????????.
	????? ???? ????????? ?????????? ???????.*/

	motor1.run(RELEASE); 
	motor2.run(RELEASE);
	motor3.run(RELEASE);
	motor4.run(RELEASE);
	delay(1000);

        motor1.run(BACKWARD);  // ?????? ???????? ?????
	motor2.run(BACKWARD);
	motor3.run(BACKWARD);
	motor4.run(BACKWARD);
	motor1.setSpeed(150);  // ?????? ???????? ???????? 
	motor2.setSpeed(150); 
	motor3.setSpeed(150); 
	motor4.setSpeed(150); 
	delay(350);
  
        motor1.run(RELEASE);
	motor2.run(RELEASE);
	motor3.run(RELEASE);
	motor4.run(RELEASE);
	delay(1000);
// ?? ???? 
        motor1.run(FORWARD); // ?????? ???????? ??????
	motor4.run(FORWARD);
	motor1.setSpeed(140); // ?????? ???????? ????????
	motor4.setSpeed(140); 
	delay(350);

	// ????????????? ?????????
	/* ????? ?? ??????????? ????? ??????????? ??????????? ???????? ??????????.
	????? ???? ????????? ?????????? ???????.*/

	motor1.run(RELEASE); 
	motor2.run(RELEASE);
	motor3.run(RELEASE);
	motor4.run(RELEASE);
	delay(5000);
  }
if (digitalRead(31) == LOW && turnCount1 <7)
{
turnCount1++;
// ?? ???? 
        motor1.run(FORWARD); // ?????? ???????? ??????
	motor4.run(FORWARD);
	motor1.setSpeed(140); // ?????? ???????? ????????
	motor4.setSpeed(140); 
	delay(350);

	// ????????????? ?????????
	/* ????? ?? ??????????? ????? ??????????? ??????????? ???????? ??????????.
	????? ???? ????????? ?????????? ???????.*/

	motor1.run(RELEASE); 
	motor2.run(RELEASE);
	motor3.run(RELEASE);
	motor4.run(RELEASE);
	delay(5000);
}
}
            digitalRead(31);

Read the state of the pin, throwing away the ONLY thing the function does. Why?

It's good to see that you grasped the concept of state machines and blink without delay. I predict you'll go far.

boguz: so step one getting rid of the delays() and step two using the state change idea to increment the count variable. a bit of work to be done... ;)

"BUT it works!" :astonished: