Hello, im a newbie here. I would like to seek help from your all on my robot coding. I have 2 limit switch between my robot leg. My goal is to let the limit switch to reverse the motor movement when the robot leg hits on it. I have done the wireless control part. My problem is i can string my key to execute the motor to move but the limit switch is unable to work. The limit switch connect like a pull up resistor circuit. Please help me out. :~
#include <SoftwareSerial.h>
int potPin = 1; // select the input pin for the voltage divider output
int val = 1; // variable to store the value coming from the sensor
//declaring pin Nos of FIRST L298D
int en1 = 5;
int en2 = 6;
int in1 = 7;
int in2 = 8;
int in3 = 9;
int in4 = 10;
//declaring pin nos of SECOND L298D
int en1o2 = 3;
int en2o2 = 11;
int in1o2 = 4;
int in2o2 = 12;
int in3o2 = 14;
int in4o2 = 15;
//int ser=0;
int sw1=24;
int sw2=26;
int motorPin[] = {
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};//array for storing pin nos
void setup() {
//initialize both serial ports:
Serial.begin(57600);
int a=(1);
for (int i = 0; i < 12; i++)
{
pinMode(motorPin[i], OUTPUT);
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
}//close for loop
}
void loop() {
if (Serial.available() > 0)
{
int ser = Serial.read();//Starts reading data from the serial monitor once condition is met
int switch1 = digitalRead(sw1);
int switch2 = digitalRead(sw2);
val = analogRead(potPin);
Serial.println(val);//potential meter used
for (int i=0; i>255; i++)
{
if(i == '1')//String key 1, motor will trigger when pressed 1
{
forward();//go forward when f is pressed
}
}
}
}
void forward()
{
int switch1 = digitalRead(sw1);//limit switch 1
int switch2 = digitalRead(sw2);//limit switch 2
if (switch1 == LOW)//Motor will move when '1' is pressed
{
//Serial.println(val);
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else if (switch1 == HIGH)
{
digitalWrite(en1,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
delay (800);
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else if (switch2 == HIGH)
{
digitalWrite(en1,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
delay (800);
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else {
}
}// close switch-case
Hi John! I manage to get it work with my hyperterminal. The problem now is when i want my limit switch to work, i need to hold on the '1' on the keyboard only the limit switch will function. If i release the '1' button on the keyboard, the motor will still running but the limit switch will not work. Someone told me to use interrupt, well i got 8 limit switch and i seek guidance from the community now. Pls help me
#include <SoftwareSerial.h>
int potPin = 1; // select the input pin for the voltage divider output
int val = 1; // variable to store the value coming from the sensor
//declaring pin Nos of FIRST L298D
int en1 = 5;
int en2 = 6;
int in1 = 7;
int in2 = 8;
int in3 = 9;
int in4 = 10;
//declaring pin nos of SECOND L298D
int en1o2 = 3;
int en2o2 = 11;
int in1o2 = 4;
int in2o2 = 12;
int in3o2 = 14;
int in4o2 = 15;
int ser=0;
int motorPin[] = {
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15};//array for storing pin nos
void setup() {
//initialize both serial ports:
Serial.begin(57600);
int a=(1);
for (int i = 0; i < 12; i++)
{
pinMode(motorPin[i], OUTPUT);
pinMode(24, INPUT);
pinMode(26, INPUT);
}//close for loop
}
void loop() {
if (Serial.available() > 0)
{
int ser = Serial.read();//Starts reading data from the serial monitor once condition is met
//int switch1 = digitalRead(sw1);
//int switch2 = digitalRead(sw2);
val = analogRead(potPin);
Serial.println(val);//potential meter used
//for (int i=0; i<255; i++)
{
if(ser == '1')//String key 1, motor will trigger when pressed 1
forward();//go forward when f is pressed
if(ser == '4')//String key 1, motor will trigger when pressed 1
pause();//go forward when f is pressed
else
ser = 0;
}
}
}
void forward()
{
int switch1 = digitalRead(24);//limit switch 1
int switch2 = digitalRead(26);//limit switch 2
if (switch1 == LOW)//Motor will move when '1' is pressed
{
//Serial.println(val);
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
if (switch2 == LOW)
{
digitalWrite(en2o2,HIGH);
digitalWrite(in3o2,HIGH);
digitalWrite(in4o2,LOW);
}
if ((switch1 == HIGH) || (ser == '1' && LOW))
{
digitalWrite(en1,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
delay (500);
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
if ((switch2 == HIGH) || (ser == '1' && LOW))
{
digitalWrite(en2o2,HIGH);
digitalWrite(in3o2,LOW);
digitalWrite(in4o2,HIGH);
delay (500);
digitalWrite(en2o2,HIGH);
digitalWrite(in3o2,HIGH);
digitalWrite(in4o2,LOW);
}
else {
switch1 = LOW;
switch2 = LOW;
}
}// close switch-case
void pause()
{
digitalWrite(en1,LOW);
digitalWrite(en2o2,LOW);
}
First of all, if you use comments (which is a good idea) make sure your comments are correct.
You have copy/pasted parts of your code, but didn't correct the comments leaving very confusing information in your sketch.
You are using functions().
Functions are parts of code that will be exclusively executed when called.
So if you do not call a function, the code will not be executed.
You are setting the motor movements in functions like forward() and pause().
These functions are called when you press a certain key on your PC's keyboard.
Pressing key 1 calls the forward() function, and the motor will be set to move forward.
The forward() function is also reading your end switch and acting accordingly.
Once you are no longer pressing that key on your PC, the function is not called anymore.
So whatever your motorshield (or whatever similar setup you are using) isn't controlled any more and will keep doing what you told it last to do.
The solution to all this, is to not have the end switch reading and processing inside that function that is only called in certain circumstances, but to have it do it's job all the time.
Also set it up in such way that if the "forward" end switch is LOW, there will never be a call to forward(), or else pressing key 1 might make your motor run forward anyway (but probably very slow).