program on simple algorithm

// I need help with this algorithm
i know how touse if else statements but dont know how to pause specific function when condition is true
in this case "dont rotate anticlockwise" and "dont rotate clockwise"

``````if button1==low
rotate anticlockwise
if button1==high
dont rotate anticlockwise
but can rotate clockwise
if button2==low
rotate clockwise
if button2==high
dont rotate clockwise
but rotate anticlockwise
if both are low
can rotate clockwise
can rotate anticlockwise

//functions

rotae clockwise
rotate anticlockwise
``````

Hi,
Can you draw up a table that has all the button combinations you have and what the motor actions you want outputted.
Are there two states, clock and anticlock, OR three with STOP as well.

Button 1 Button 2 Motor State
0 0
1 0
1 1
0 1

Just tell us the outputs you need.
You may be overthinking the process and what simple coding logic can do.

Thanks.. Tom...

// here are the motor state

button 1 button 2 motor state

0 0 stop

1 0 rotate clockwise

0 1 rotate anticlockwisse

1 1 stop

HERE is the basic design

here button 1 and button 2 are acting as a limit switch

if any button is pressed with a robot arm is not able to move further respective to its direction

it is basically robot arm controlled by Arduino

Hi,
Thanks for the info and that makes it clear, however if they are limit switches, if both are "0", then you should be able to turn in either direction as the arm is not against a stop.

Tom..

Hi,
Can you post your code please and we can see how to integrate the endstops into it?

Thanks.. Tom...

yes if both are zero then they can move in any direction

I have designed two codes

• one is manual with push-button and Arduino

*and another is with esp32 and blynk

``````//this is Arduino and push button code

const int button1=2;
const int button2=4;
const int button_brake1=7;
const int button_brake2=8;

int  button1_state=0;
int  button2_state=0;
int  button_brake1_state=0;
int  button_brake2_state=0;

void setup() {
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(4,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
}

void loop()
{

if (button_brake1_state==LOW){
if (button1_state==HIGH){
arm_forward();
Serial.print("button forward=");
Serial.println(button1_state);
}
else if (button_brake1_state==HIGH){
arm_forward_brake();
Serial.print("button brake 1=");
Serial.println(button_brake1_state);
}
}
else if{
if ( button2_state==HIGH){
arm_backward();
Serial.print("button backward=");
Serial.println(button2_state);
}
else if (button_brake2_state==HIGH){
arm_forward_brake();
Serial.print("button brake 1=");
Serial.println(button_brake1_state);
}
}
else{
arm_stable_mode();
Serial.print("arm is in stable mode\n");
}
}
void arm_forward(){
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
}
void arm_backward(){
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
}
void arm_forward_brake(){
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
void arm_backward_brake(){
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
void arm_stable_mode(){
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
``````
``````// this is esp32 and blynk code
// please note here I have used multiple devices and motor drivers

#define BLYNK_PRINT Serial

// Include Libraries
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32_Servo.h>

//pins for motor driver1 inputs
const int input1=32;
const int input2=33;
const int input3=25;
const int input4=26;

//pins for motor driver2 inputs

const int input11=27;
const int input22=14;
const int input33=12;
const int input44=13;

//pins for motor driver3 inputs

const int MotorLeft[2] = {23,22};
const int MotorRight[2] = {1,3};

//pins for limit (brake) switches
const int upper_limit=36;
const int lower_limit=39;

//pins for other components
const int buzzer_pin=4;

// Pin Definitions for servos
#define SERVOMD2_2_PIN_SIG  5
#define SERVOMD4_4_PIN_SIG  18

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "khaNno_d_PlPu9JRoLDk8mOyCzYmO_ec";

// Set password to "" for open networks.
char ssid[] = "lgq6";
char pass[] = "avanish307";

WidgetLED led1(2);

BlynkTimer timer;

// 2 LED Widget is blinking
{
if (led1.getValue()) {
led1.on();

}
else {
led1.off();

}
}

Servo servoMD2_2;
Servo servoMD4_4;

BLYNK_WRITE(V0)
{
servoMD2_2.write(param.asInt());
}
BLYNK_WRITE(V1)

{
servoMD4_4.write(param.asInt());
}

int arm_brake1=0;
int arm_brake2=0;

void setup() {
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");
Blynk.begin(auth, ssid, pass);
MotorInit();
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

servoMD2_2.attach(SERVOMD2_2_PIN_SIG);
servoMD4_4.attach(SERVOMD4_4_PIN_SIG);

pinMode(36,INPUT);
pinMode(39,INPUT);
pinMode(32,OUTPUT);
pinMode(33,OUTPUT);
pinMode(25,OUTPUT);
pinMode(26,OUTPUT);
pinMode(27,OUTPUT);
pinMode(14,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(4,OUTPUT);

}

void loop() {

if (arm_brake1==1){
lower_joint_brake();
Serial.print(" arm_brake 1=");
Serial.println(arm_brake1);
}
else if
(arm_brake2==1){
upper_joint_brake();
Serial.print(" arm_brake 2=");
Serial.println(arm_brake2);
}
else{
arm_stable_mode();

}

Blynk.run();
timer.run();

}
//Intialize the motor
void MotorInit()
{
int i;
for(i=0 ; i<2; i++)
{
pinMode(MotorLeft[i],OUTPUT);
pinMode(MotorRight[i],OUTPUT);
}
}
//Robot Driving Functions
void Robot_Forward()
{
digitalWrite(MotorLeft[0],0);
digitalWrite(MotorLeft[1],1);
digitalWrite(MotorRight[0],1);
digitalWrite(MotorRight[1],0);
}
void Robot_Backward()
{
digitalWrite(MotorLeft[0],1);
digitalWrite(MotorLeft[1],0);
digitalWrite(MotorRight[0],0);
digitalWrite(MotorRight[1],1);
}
void Robot_Left()
{
digitalWrite(MotorLeft[0],1);
digitalWrite(MotorLeft[1],0);
digitalWrite(MotorRight[0],1);
digitalWrite(MotorRight[1],0);
}
void Robot_Right()
{
digitalWrite(MotorLeft[0],0);
digitalWrite(MotorLeft[1],1);
digitalWrite(MotorRight[0],0);
digitalWrite(MotorRight[1],1);
}
void Robot_Stop()
{
digitalWrite(MotorLeft[0],0);
digitalWrite(MotorLeft[1],0);
digitalWrite(MotorRight[0],0);
digitalWrite(MotorRight[1],0);
}

//arm joint movement functions
void lower_joint_forward(){
digitalWrite(32,1);
digitalWrite(33,0);

}
void lower_joint_backward(){
digitalWrite(32,0);
digitalWrite(33,1);

}
void lower_joint_brake(){
digitalWrite(32,0);
digitalWrite(33,0);
}
void upper_joint_forward(){
digitalWrite(25,1);
digitalWrite(26,0);

}
void upper_joint_backward(){
digitalWrite(25,0);
digitalWrite(26,1);

}
void upper_joint_brake(){
digitalWrite(25,0);
digitalWrite(26,0);
}
void arm_stable_mode(){
digitalWrite(32,0);
digitalWrite(33,0);
digitalWrite(25,0);
digitalWrite(26,0);
digitalWrite(27,0);
digitalWrite(14,0);
digitalWrite(12,0);
digitalWrite(13,0);
}
void output1_1(){
digitalWrite(27,1);
digitalWrite(14,0);
}
void output2_1(){
digitalWrite(14,1);
digitalWrite(27,0);
}
void output3_1(){
digitalWrite(12,1);
digitalWrite(13,0);
}
void output4_1(){
digitalWrite(13,1);
digitalWrite(12,0);
}
BLYNK_WRITE(V2)
{
int value = param.asInt(); // Get value as integer
Serial.println("Going Forward");
if(value)
{
Robot_Forward();

}
}

BLYNK_WRITE(V3)
{
int value = param.asInt(); // Get value as integer
Serial.println("Moving Right");
if(value)
{
Robot_Right();
delay(200);
Robot_Stop();
}
}

BLYNK_WRITE(V4)
{
int value = param.asInt(); // Get value as integer
Serial.println("Going back");
if(value)
{
Robot_Backward();

}
}

BLYNK_WRITE(V5)
{
int value = param.asInt(); // Get value as integer
Serial.println("Taking Left");
if(value)
{
Robot_Left();
delay(200);
Robot_Stop();

}
}
BLYNK_WRITE(V6)
{
int value = param.asInt(); // Get value as integer
Serial.println("Braking!!");
if(value)
{
Robot_Stop();
}
}
BLYNK_WRITE(V7)
{
int value = param.asInt(); // Get value as integer
Serial.println("lower_joint_forward");
if(value==1)
{
lower_joint_forward();
}
}
BLYNK_WRITE(V8)
{
int value = param.asInt(); // Get value as integer
Serial.println("lower_joint_backward");
if(value)
{
lower_joint_backward();
}
}
BLYNK_WRITE(V9)
{
int value = param.asInt(); // Get value as integer
Serial.println("upper_joint_forward");
if(value)
{
upper_joint_forward();
}
}
BLYNK_WRITE(V10)
{
int value = param.asInt(); // Get value as integer
Serial.println("upper_joint_backward");
if(value)
{
upper_joint_backward();
}
}
BLYNK_WRITE(V11)
{
int value = param.asInt(); // Get value as integer
Serial.println("output1_1");
if(value)
{
output1_1();
}
}
BLYNK_WRITE(V12)
{
int value = param.asInt(); // Get value as integer
Serial.println("output2_1");
if(value)
{
output2_1();
}
}
BLYNK_WRITE(V13)
{
int value = param.asInt(); // Get value as integer
Serial.println("output3_1");
if(value)
{
output3_1();
}
}
BLYNK_WRITE(V14)
{
int value = param.asInt(); // Get value as integer
Serial.println("output4_1");
if(value)
{
output4_1();
}
}
BLYNK_WRITE(V15)
{
int value = param.asInt(); // Get value as integer
Serial.println("BUZZER IS ON");
if(value)
{
digitalWrite(4,1);
}
}
``````

Hi,
Stick with the Arduino for the moment, what model Arduino are you using?
How have you got the switches wired and do you have pulldown resistors on the input pins of the controller.

You haven't said how big or powerful your motor and mechanicals are?

Tom...

i am using l298n motor driver it only requires two inputs on for 1 motor and the logics are provided

i have tested this code but got the error that if any of the button is high(pressed) it stops the entire rotation both clock and anticlockwise

and no pulldown resistors are used

Hi,
Can you post a circuit diagram please?

i have tested this code but got the error that if any of the button is high(pressed) it stops the entire rotation both clock and anticlockwise

You need to have 10k pull down resistors, but lets see your circuit diagram first.

Thanks.. Tom..

here is the schematic

two buttons are for brake
two buttons are to control movement in a particular direction

two wires not connected to anything is connected to l298 input

here is the diagram

hello tom are you still there???

Hi,
Ops circuit.

Which inputs are the limit switches?
Do you have buttons to make the motor go clock and anti-clock?
What and why do you have two brake buttons?

Thanks.. Tom..

Declaring a pin as INPUT requires it to be connected to either 5V or GND when read. Otherwise it will be floating, and digitalRead() will return arbitrary values.

To solve this you have to add a resistor (10k, or so, ref TomGeorge in #11) between each input-pin and GND to force them LOW when a button is not pressed. When pressed it will connect to 5V and read HIGH.

Alternatively you can declare the inputs as INPUT_PULLUP and connect the switches to GND instead of 5V. The logic then becomes inverse, and a button will read LOW when pressed, and HIGH when not.