Merging 2 code - yanker

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 also have issue with my arm and cannot take vibration, reason for this project :smiley: any code is fine (sorry if not allowed)

I moved your question to a better category, it was not a product showcase.

instead of trying to put together codes from various sources, try to describe exactly the requirements and what you want to build.

start with plain English, be detailed when it comes to the various operations

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!

Hello bbud2

Welcome to the world's best Arduino forum ever.

Consider this small example using function pointer to call the programs selected by the switches.

enum SwitchNames {One, Two};
constexpr uint8_t SwitchPins[] {A0, A1};
void (*programs[])() {
  program0, program1, program2, program3
};

void setup()
{
  Serial.begin(115200);
  Serial.println("start");
  for (auto SwitchPin : SwitchPins) pinMode(SwitchPin, INPUT_PULLUP);
}
void loop()
{
  programs[((digitalRead(SwitchPins[Two]) ? LOW : HIGH) << 1) + (digitalRead(SwitchPins[One]) ? LOW : HIGH)]();
}
//--------------- your programs here  ---------------------------
void program0()
{
  Serial.println(__func__);
  delay(1000); // for testing only
}
void program1()
{
  Serial.println(__func__);
  delay(1000); // for testing only
}
void program2()
{
  Serial.println(__func__);
  delay(1000); // for testing only
}
void program3()
{
  Serial.println(__func__);
  delay(1000); // for testing only
}
//---------------------------------------------------

Have a nice day and enjoy coding in C++.

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

1 Like

magform

If you prefer fast, nice and wrong. I would say, the act of helping is kind.

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)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.