Connect button and motor at Arduino and press button sequentially.
I'm going to do something where the motor moves sequentially.
For example, if you have button 1 (motor rotates left), 2 (motor rotates right), and you have one motor connected to it
After pressing the buttons 1, 2, 1, 1 and 2, you want to implement the motor moving left, right, left, left and right.
When you press the button, you receive the value and store it in the array to move the motor.
I wonder how I can save the pin input value to the array.
arduino
You must use a data structure called Queue, where first incoming commands are processed first. Below is a code that implements a queue that can store MAX(3) inputs. You can modify the number of MAX as you want and use it.
#include <stdio.h>
#define MAX 3
#define BLANK -1111111
#define LEFT -1
#define RIGHT 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);
printf("Read %d\n",ret);
return ret;
}
}
printf("Nothing to read\n");
return BLANK;
}
void reset(){
rear = 0;
front = - 1;
for(int i=0;i<MAX;i++){
queue_array[i]=BLANK;
}
}
int main()
{
reset();
insert(LEFT);
printf("(%d,%d)\n",front,rear);
insert(LEFT);
printf("(%d,%d)\n",front,rear);
insert(RIGHT);
printf("(%d,%d)\n",front,rear);
insert(LEFT);
read();
read();
read();
read();
insert(RIGHT);
read();
for(int i=0;i<MAX;i++){ printf("%d(%d,%d)\n",queue_array[i],front,rear);
}
return 0;
}
Using the queue implemented by the code above, if there is an input from the loop, you can queue it, and then use read to process the commands in order.
void loop() {
leftButtonState = digitalRead(leftButtonPin);
if (leftButtonState != lastLeftButtonState) {
insert(LEFT);
}
The same is true of //right. Insert the input.
//Use read() to process the input.
}
If the goal is to enter the sequence in advance and make it work when you press OK...
// Define action status for front, back, left, and right
#define FORWARD 1
#define BACKWARD 2
#define LEFT 3
#define RIGHT 4
void setup() {
// Initialize
// Queue initialization
}
void loop() {
// Read button(s) status
If (if forward button) {
// Add FORWARD to queue. Create and write a welcome cue or write Jung Doo-sik's cue implementation.
// The following example is illustrated based on Jung Doo-sik's implementation of the queue.
insert(FORWARD);
} else if (if reverse) {
insert(BACKWARD);
} else if (if left) {
insert(LEFT);
} else if (right) {
insert(RIGHT);
} // The last time you press OK, actually take action.
else if (if the OK button is pressed) {
while(((action=read())!=BLANK) // If the queue is not empty, continue.
{
if( action == FORWARD ) {
// Motor motion - forward.
} } else if( action == BACKWARD ) {
// Motor motion - reverse.
} } else if ( action == LEFT ) {
// Motor Operation - Left
} } else if ( action == RIGHT ) {
// Motor Operation - Right
}
}
// Reset the cue when you're done.
reset();
}
}
I think we can do it like above.
However, you will not be able to enter a new button until you press OK above and finish all input sequences.
Currently, the program is implemented as shown below, so it is not a sequential movement
If you press it, it's acting in the form of it.
I'm modifying it based on the answers you posted, for example, the part about the movement
digitalWrite(in1pin, HIGH);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, HIGH);
The part of the motor that rotates forward on one side and backward on the other side to the right
It is difficult to correct referring to the code above...ㅠ<
Currently implemented full code
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 = 0;
int buttonStateGO = 0;
int buttonStateBACK = 0;
int buttonStateLEFT = 0;
int buttonStateRIGHT = 0;
void setup() {
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(in1pin, OUTPUT);
pinMode(in2pin, OUTPUT);
pinMode(in3pin, OUTPUT);
pinMode(in4pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonStateOK = digitalRead(buttonPin0);
buttonStateGO = digitalRead(buttonPin1);
buttonStateBACK = digitalRead(buttonPin2);
buttonStateLEFT = digitalRead(buttonPin3);
buttonStateRIGHT = digitalRead(buttonPin4);
if (buttonStateGO == HIGH) {
digitalWrite(in1pin, HIGH);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, HIGH);
digitalWrite(in4pin, LOW);
delay(1500);
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, LOW);
} } else if(buttonStateBACK == HIGH) {
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, HIGH);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, HIGH);
Serial.println("back!");
delay(2000);
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, LOW);
} } else if(buttonStateLEFT == HIGH) {
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, HIGH);
digitalWrite(in3pin, HIGH);
digitalWrite(in4pin, LOW);
delay(995);
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, LOW);
} } else if(buttonStateRIGHT == HIGH) {
digitalWrite(in1pin, HIGH);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, HIGH);
delay(995);
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, LOW);
} } else {
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, LOW);
digitalWrite(in3pin, LOW);
digitalWrite(in4pin, LOW);
}
}
Thank you.
© 2024 OneMinuteCode. All rights reserved.