Please do not use hijack my simulation work to further whatever your goals are.
We are trying to show @jmathew213123 that there is nothing hard about limit switches.
You have needlessly obscured the logic. The hole point was to make the OP actually read, and understand, code she might be able to. Read and understand.
So I tested that new code that I made and it actually worked for the most part. When I initially upload the code, the motor starts moving right away before I press the start button. For some reason both the limit switches work correctly as when I press the first limit switch the motor spins counterclockwise and when I press the second limit switch it stops the motor as it should. For some reason the button isn't working. The button may be faulty but it could also be a coding error. If it is a coding error I'm not sure what I did incorrect. I'm think it could be something to do with the debounce stuff I included from the example code @alto777 showed me but I'm not sure.
Did you try a simple sketch that proves the button is working or not working?
You have to adopt an experimenter's or scientific or even just common sense approach to figuring out what is happening and why it isn't what you think.
How is that button wired? Did you ever say what the button is supposed to do?
Why is the button pin mode INPUT? Could it be you've left off a resistor to pull it, either way, or that you've used the wrong sense on the button, HIGH means what, presst or not presst?
Did you not read and trace either simulation to see that debouncing does not need to be part of the solution? The mechanical constraints of the physical system are such debouncing is unnecessary.
You ended up with three copies of a common debouncing pattern, all running off one timer.
I renamed the timer lastInputCheckTime to better reflect its roll. I removed the extra conditions on the button and limit switch debouncing code. You were saying "see this press only if it is the only thing being pressed", which is not necessary. The two limit switches cannot be "pressed" at the same time. If the button happened to be pressed at the exact (within 50 ms) of either limit switch, so what? The two switches would be handled in the order they were pressed, or if exact, in the order the code says to check. Perhaps moving the button debouncing code to be the last of three would give you comfort as it would then "prioritize" that function.
I also removed all but one call to millis(). I call it once, and stash that as now, and use now anywhere I need the current time. So everyone works off the same clock. You'll see ppl doing that and dignifying it by using currentMillis or something fancy like that.
Read the code:
#include <Servo.h>
Servo servo;
#define BUTTON_PIN 8
#define LIMIT_PIN 7
#define LIMIT_PIN2 6
byte lastButtonState = LOW;
byte lastLimitState = HIGH;
byte lastLimitState2=HIGH;
unsigned long debounceDuration = 50; // millis
unsigned long lastInputCheckTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("state toggle blur");
servo.attach(9);
pinMode(BUTTON_PIN, INPUT);
pinMode(LIMIT_PIN, INPUT_PULLUP);
pinMode(LIMIT_PIN2, INPUT_PULLUP);
}
void loop() {
static unsigned long now;
now = millis(); // one millis call for all time
if (now - lastInputCheckTime > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
byte limitState = digitalRead(LIMIT_PIN);
byte limitState2 = digitalRead(LIMIT_PIN2);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == HIGH) {
Serial.println("sixty");
servo.write(60);
}
lastInputCheckTime = now;
}
if (limitState != lastLimitState) {
lastInputCheckTime = now;
lastLimitState = limitState;
if (limitState == LOW) {
Serial.println(" 120");
servo.write(120);
}
lastInputCheckTime = now;
}
if (limitState2 != lastLimitState2) {
lastInputCheckTime = now;
lastLimitState2 = limitState2;
if (limitState2 == LOW) {
Serial.print(" 60... ");
servo.write(60);
delay(1000);
Serial.println(" ... 180");
servo.write(180);
}
lastInputCheckTime = now; // delay kinda messes the hole idea up, but hey.
}
}
}
So I have tried both a7's code and my code without the denouncing stuff and in both instances when I upload the program the motor starts moving clockwise. I have change the button multiple times and have eve tried using a vex button but it does that every time. It doesn't seem to detect the button for some reason. Both the limit switches work just fine so I'm not sure why the button won't I have check all my wiring so I don't think that is the issue. I'm not sure what I'm missing in my code.
I have wired the button in what I believe is pull down. I am using a 10k ohm resistor which is wired to the ground pin on the button. When I tested it in the past it gave a low signal when untouched and a high when pressed. I have been wiring it the same way since I start this project.
Both of my limit switches are wired without any resistors. I use INPUT_PULLUP in my code. The button is wired in a pull down method. I use a 10k ohm resistor and it is wired to ground. I'm not sure how the wiring could be the issues because these have both worked separately before so why wouldn't they work together. Do I need to not use any resistors at all and change the coding of the button. Would using INPUT_PULLUP on the button fix the issue?
We are at post 166. How many additionaly posts do you need to understand that posting a working code does not happen??
Here is
exactly
your code-logic
I just added what each and every arduino-coder does:
I added serial output that makes visible what your code is doing.
How many additional postings do you need to understand that printing to the serial monitor is the best analysing tool that you have??
upload this code-version and open the serial monitor
adjust baudrate to 115200 and then watch and read what gets printed to the serial monitor
Then you will exactly see what your code is doing and why it does not work as you intended.
So I think the issue was actually the Arduino pin. When I ran the test code with the button plugged into pin 8 the serial monitor showed it output a constant high signal. When I put it into pin 7 it gave me a low signal when unpressed and a high signal when pressed. I'll now try it in my main program
My system now works. I guess the issue was just that pin. Here is the test code I used.
#define BUTTON_PIN 8
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(BUTTON_PIN) == LOW){
Serial.println("low");
}
if (digitalRead(BUTTON_PIN)==HIGH){
Serial.println("high");
}
}
My next issue is making the button wireless. To do this I think I need to use a transmitter and receiver. I've done a bit of research and I found that there are two different types. 315 hz and 433hz. I think I'll use 433 since it is stronger and the button will be outside the vending machine and the rest will be inside. I'm not sure which one to get however. I watched a video and they used cheaper pair. I looked in the comments of that video and they recommended to use a Superheterodyne Receiver since it is more reliable. However in that video he imported some code using a website called radio head and said it was important part of making it work. I'm not sure what transmitter and receiver to buy or if they will work with this radio head website. Do I even need to use that website.
video I watched
possible transmitter and receiver option 1 option 2 (not alot of review on this so I'm not sure if I want to go with this)
Is the RF thing really the only next step? I ask because you are wading towards deeper waters… I suggest you work on something else and…
Please do not just try to add new functions or hardware or modules until you have played with them in simple circumstances, like coming to a full understanding of example code you had no part in writing.