Hello, I have a project which involves a solar panel stand that moves either using a joystick (manual control code) or light sensors (autonomous control code). Two servo motors are used: one for up-down movement, one for left-right movement. The manual and auto codes work perfectly by themselves. In the combined code, I want it to function so that when the digital pin #2 sees a HIGH input, either the manual or auto code works, and when it sees a LOW, the other code works. I have a button that will provide either constant HIGH or LOW. Right now, when the combined code is uploaded, the servos flip out, shake, and are overall really unhappy. Also, no functions work, it seems as if the manual code and auto codes are fighting each other. I'm pretty sure I'm missing something minor, I'm just not sure what since I am fairly new to arduino and C++. All codes I have worked with are hopefully attached correctly. Any help would be great, Thank You.
int ldrlt = 0; //Bottom Left Light Sensor
int ldrrt = 1; //Bottom Right Light Sensor
ldr in the name means Light Dependent Resistor, right?
l or r means left or right, right?
And, t means bottom? Really?
int ldrld = 2; //Top Left Light Sensor
int ldrrd = 3; //Top Right Light Sensor
So, d mean top?
digitalRead(2);
if(inPin == HIGH)
Read the state of the pin, and through the result away. After all, who cares whether the switch is pressed or not, since you are only going to compare the pin NUMBER to HIGH or LOW.
Ah sorry, that was my mistake with the commenting. Either way, Robin2 is right on with the problem in my code. The duplication in the setup is causing a big issue but im not sure how to change it since both codes are different in how they go about attaching the servos and the other functions throughout. Is there any way to make a simple change to the setup so that it works without having to make major changes to one of the codes? I have been trying to change up the manual control code to look more like the auto code but have had little success. Thank you for your help in advance.
but im not sure how to change it since both codes are different in how they go about attaching the servos and the other functions throughout
But, there is no reason for that. Pick ONE method, and stick with it. Either you want an array of servos, pins, and positions, or you don't. There is no reason to attach the servos twice, to the same pins.
Thank you for your response. I do understand that I am attaching the servos twice but my issue is making it so that the setup for both codes are satisfied in the one combined code without having to change anything else. I am unsure how to do this due to having little arduino and C++ knowledge which is why my questions/answers may seem off. The manual code uses the arrays im assuming because it is the easiest way to make the servos hold position when you let off the thumbstick. I am unsure how to use any other method. If i can combine the setups, the code should have no problem working. Im just not sure how.
You have two choices for how to store two values, whether the values are Servo instances, pin numbers, or servo positions. One way is to use two variables, like horizontal and vertical, 10 and 11, and 180 and 45 (OK, the last pairs are not variables).
The other way is to use an array, like
Servo servo[2]; //code used for attaching up to 2 servos
byte angle[2] = {240, 19}; // middle point for servo angle
byte servoPin[2] = {9, 10}; // input pins to attach servos
You use both methods in your code. You need to stop that. Pick ONE. Which ONE is up to you.
Once you have picked ONE, and told us which ONE you want to use, we can help you fix the code that uses the other method to use the ONE that you chose.
The first step is for you to tell us which ONE you chose. With the exception of setting the initial position of the servos, ManualControl() and AutonomousControl() are doing the SAME thing.
Pick which one you want to keep, based on the ONE method you chose to store the data, and delete the other one.
Thank you for the explanation. I would prefer to have the entire code using variables to set and store data like those used in the autonomous control code. I attempted to convert the manual code to using this method but was unsuccessful.
I would prefer to have the entire code using variables to set and store data like those used in the autonomous control code.
OK. So, unroll the for loops in loop. You will have one block of code that uses angle[0] and servo[0] and one that uses angle[1] and servo[1]. Change all occurrences of angle[0] to servov and change all occurrences of angle[1] to servoh.
Change all occurrences of servo[0] to vertical and change all occurrences of servo[1] to horizontal.
Thank you. I attached below my first attempt at converting it using your advise you just gave. I left most of the previous code in their commented out. I am getting errors of variables not being declared but I think a bigger problem might be how I unrolled the loops. Would I have to break the original "for" loops into two "for" loops? For example, have "for (vertical) {...}" and then "for (horizontal) {...}?"
Disregard my previous post, using your suggestions my code now works as desired. I have attached the revised code below. I have yet to place it in the combined code but when I do I will place that hear as well for anyone else to use if desired. Thank you so very much. You have made hours and hours of troubleshooting worth it.