Hello I am trying to put make a yanker with 4 ''program'' controlled with 2 switch.
I hook a cylinder to a servo and Using a Uno (couldnt get the loader running, yet) 2 button switch and a small board. Hoping to learn more, but i'd like help with this issue(programing).
button code then servo code
byte switch0 = 2; // wire switch to connect to Gnd
byte switch1 = 3; // wire switch to connect to Gnd
byte programToRun;
void setup(){
pinMode (switch0, INPUT_PULLUP);
pinMode (switch1, INPUT_PULLUP);
}
void loop(){
// read switches and calculate what to run, am sure there are better ways to do this. See example below.
if (digitalRead(switch1) == HIGH){
bit1 = 2;
}
else {
bit1 0 ;
}
if (digitalRead(switch0) == HIGH){
bit0 = 1;
}
else {
bit0 = 0;
}
programToRun = bit1 + bit0; // results in 3,2,1,0
// perhaps programToRun = (PIND >> 2) & 0b00000011; // shift bits 3,2 into position 1, 0, mask off the rest
switch (programToRun){
case 0:
//code for 'program 0'
break;
case 1:
// code for 'program 1'
break;
case 2:
// code for 'program 2'
break;
case 3:
// code for 'program 3'
break;
} // end switch
} // end loop
#include <Servo.h>
//www.elegoo.com
//2018.12.19
#include <Servo.h>
Servo myservo;
void setup(){
myservo.attach(9);
myservo.write(55);// move servos to center position -> 90°
}
void loop(){
myservo.write(55);// move servos to center position -> 90°
delay(50);
myservo.write(22);// move servos to center position -> 60°
delay(50);
}
I'm not entirely certain about your objective, but I'm here to assist as best I can.
First of all remember to always break down your code into functions. This practice significantly enhances readability and organization.
Below is a small code snippet that, if I comprehend your request correctly, should fulfill what you're looking for. Unfortunately, I don't have access to an Arduino at the moment, so I'm unable to test it.
#include <Servo.h>
byte switch0 = 2; // wire switch to connect to Gnd
byte switch1 = 3; // wire switch to connect to Gnd
Servo myservo;
//utility function definition
byte readSwitches();
void executeProgram(byte programToRun);
void program0();
void program1();
void program2();
void program3();
void setup() {
pinMode(switch0, INPUT_PULLUP);
pinMode(switch1, INPUT_PULLUP);
myservo.attach(9);
myservo.write(90); // move servos to center position (90°)
}
void loop() {
executeProgram(readSwitches());
}
//Utility function declaration
// Function to read the state of switches and determine the program
byte readSwitches() {
byte bit1 = digitalRead(switch1);
byte bit0 = digitalRead(switch0);
return (bit1 << 1) | bit0; // Combine bits to get program number (0-3)
}
// Function to execute the selected program
void executeProgram(byte programToRun) {
switch (programToRun) {
case 0:
program0();
break;
case 1:
program1();
break;
case 2:
program2();
break;
case 3:
program3();
break;
default:
// Handle unexpected cases, if any
break;
}
}
void program0() {
// Code for 'program 0'
}
void program1() {
// Code for 'program 1'
}
void program2() {
// Code for 'program 2'
}
void program3() {
// Code for 'program 3'
}
If there's more clarification you need or any specific questions, feel free to ask!
when I added this part to the code it wouldnt work.
void program1() {
void loop()
myservo.write(55);// move servos to center position -> 90°
delay(50);
myservo.write(22);// move servos to center position -> 60°
delay(50);
// Code for 'program 1'
}
my goal would be to be able to adjust the speed and degree, by having 4 program activated by 2 switch button. In a loop, so the 'program' should keep running until a other switch is activated or the device is turned off.
In your specific case probably you need to have something like:
void program1() {
myservo.write(55);// move servos to center position -> 90°
delay(50);
myservo.write(22);// move servos to center position -> 60°
delay(50);
}
I know that what I am about to suggest is quite controversial but don't be afraid to use chatGPT for this type of issue as long as you can fully understand what he is doing and if you don't understand, ask him for an explanation.
If you prefer to ask on the form or something else, maybe you can get some more advice but sometimes you have to wait a long time and people are not always kind.
Let me know if this code works and if you need something else
That’s the crux - usually people don’t understand when cheatGPT is wrong and usually when it gets wrong it does not correct itself well.
Also beginners have a hard time specifying the needs in a proper algorithmic way as a proper state machine (if they were able to do so they could solve their issue themselves)