Action question after saving Arduino array.

Asked 2 years ago, Updated 2 years ago, 88 views

I'm trying to implement it based on the questions you asked and answered I'm struggling because I can't understand the expression of the queue accurately...

#define FORWARD 1
#define BACKWARD 2
#define LEFT 3
#define RIGHT 4
#define MAX 10

constant buttonPin0 = 7; // Start button
constant buttonPin1 = 8; //front button
constant buttonPin2 = 9; //Back button
constint buttonPin3 = 10; // right button
constant buttonPin4 = 11; // Left button

constint in1pin = 2; //left motor
constint in2pin = 3; //left motor
constint in3pin = 4; // right motor
constint in4pin = 5; // right motor

int buttonStateOK = 1;
int buttonStateGO = 1;
int buttonStateBACK = 1;
int buttonStateLEFT = 1;
int buttonStateRIGHT = 1;
int queue_array[MAX];
int rear = 0;   //Position to write
int front = -1;//Position to read

int nextPosition(int position) {
  return (position + 1) % MAX;
}

void insert(int add_item)
{
  if (queue_array[rear] == BLANK)
  {
    if (front == -1)
    {
      front = 0;
    }
    queue_array[rear] = add_item;
    rear = nextPosition(rear);
    printf("insert %d\n", add_item);
  }
  else {
    printf("insert %d ignored\n", add_item);
  }
}
int read() {
  if (front >= 0) {
    if (queue_array[front] != BLANK) {
      int ret = queue_array[front];
      queue_array[front] = BLANK;
      front = nextPosition(front);
      return ret;
    }
  }
  return BLANK;
}
void reset() {
  rear = 0;
  front = -1;
  for (int i = 0; i<MAX; i++) {
    queue_array[i] = BLANK;
  }
}

void setup() {
  int queue_array[MAX];
  pinMode(buttonPin0, INPUT_PULLUP);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  pinMode(in1pin, OUTPUT);
  pinMode(in2pin, OUTPUT);
  pinMode(in3pin, OUTPUT);
  pinMode(in4pin, OUTPUT);
}

void loop() {
  // Read button(s) status
  buttonStateOK = digitalRead(buttonPin0);
  buttonStateGO = digitalRead(buttonPin1);
  buttonStateBACK = digitalRead(buttonPin2);
  buttonStateLEFT = digitalRead(buttonPin3);
  buttonStateRIGHT = digitalRead(buttonPin4);
  buttonStateSTART = digitalRead(buttonPin5);

  if (buttonStateGO == LOW) {
    insert(FORWARD);
  }
  else if (buttonStateBACK == LOW) {
    insert(BACKWARD);
  }
  else if (buttonStateLEFT == LOW) {
    insert(LEFT);
  }
  else if (buttonStateRIGHT == LOW) {
    insert(RIGHT);
  }
  else if (buttonStateOK == LOW) {
    while (read() != BLANK) {
      if (read() == FORWARD) {
        // Motor Operation - Forward
        digitalWrite(in1pin, HIGH);
        digitalWrite(in2pin, LOW);
        digitalWrite(in3pin, HIGH);
        digitalWrite(in4pin, LOW);
        delay(1500);
        digitalWrite(in1pin, HIGH);
        digitalWrite(in2pin, HIGH);
        digitalWrite(in3pin, HIGH);
        digitalWrite(in4pin, HIGH);
      } } else if (read() == BACKWARD) {
        // Motor motion - reverse.
        digitalWrite(in1pin, LOW);
        digitalWrite(in2pin, HIGH);
        digitalWrite(in3pin, LOW);
        digitalWrite(in4pin, HIGH);
        delay(2000);
        digitalWrite(in1pin, HIGH);
        digitalWrite(in2pin, HIGH);
        digitalWrite(in3pin, HIGH);
        digitalWrite(in4pin, HIGH);
      } } else if (read() == LEFT) {
        // Motor Operation - Left
        digitalWrite(in1pin, LOW);
        digitalWrite(in2pin, HIGH);
        digitalWrite(in3pin, HIGH);
        digitalWrite(in4pin, LOW);
        delay(995);
        digitalWrite(in1pin, HIGH);
        digitalWrite(in2pin, HIGH);
        digitalWrite(in3pin, HIGH);
        digitalWrite(in4pin, HIGH);
      } } else if (read() == RIGHT) {
        // Motor Operation - Right
        digitalWrite(in1pin, HIGH);
        digitalWrite(in2pin, LOW);
        digitalWrite(in3pin, LOW);
        digitalWrite(in4pin, HIGH);
        delay(995);
        digitalWrite(in1pin, HIGH);
        digitalWrite(in2pin, HIGH);
        digitalWrite(in3pin, HIGH);
        digitalWrite(in4pin, HIGH);
      }
    }
    reset();
  }
}

Please give me some advice.

arduino

2022-09-22 15:32

1 Answers

First of all, I think you need to understand Q.

We're using a queue to take out the input value and write it down one by one. Please refer to the following link.

http://www.logonluv.com/2015/02/datastructure-queue.html?m=1

The code I uploaded last time is an array of simple circulation queues.


2022-09-22 15:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.