Hello, I am fairly new to programming Arduino, and this project I have been working on controls six servo and 1 stepper with Arduino Uno. I am looking for some "best practice" guidance, so as I choose to expand or change Controller (possibly to ESP32) I can do so easily.
Should I call all variables Globally or is it best co declare them in the loop also pins I don't understand why the N64 Controller Pin is called before setup and all the servos are called in void setup.
Here is the code I am working on, I have also played with arrays for all the buttons and position and pin variables.
#include <Stepper.h>
#include <Servo.h>
#include <N64Controller.h>
//Name servo motors
Servo s1;
Servo s2;
Servo s3;
Servo s4;
Servo s5;
Servo s6;
//Variables
int pos1 = 90; // Servo1 position variable
int pos2 = 90; // Servo2 position variable
int pos3 = 90; // Servo3 position variable
int pos4 = 90; // Servo4 position variable
int pos5 = 90; // Servo5 position variable
const int RPM = 15; // Stepper RPM
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
//Arduino Pins
N64Controller Ncontrol(12); // N64 Controller pin
const int s1_pin = 7; // Servo 1 Pin
const int s2_pin = 5; // Servo 2 Pin
const int s3_pin = 6; // Servo 1 Pin
const int s4_pin = 9; // Servo 2 Pin
const int s5_pin = 10; // Servo 1 Pin
const int s6_pin = 11; // Servo 2 Pin
Stepper myStepper(stepsPerRevolution, 1, 3, 2, 4); // Stepper motor pins
void setup() {
Ncontrol.begin(); // Initialisation
Serial.begin(9600); // Start Serial communication
myStepper.setSpeed(RPM); // Set stepper speed
s1.attach(s1_pin);
s2.attach(s2_pin);
s3.attach(s3_pin);
s4.attach(s4_pin);
s5.attach(s5_pin);
s6.attach(s6_pin);
// s1.write(pos);
// s2.write(pos);
}
void loop() {
delay(5);
//Get a reading from the controller and place it into variables
Ncontrol.update();
int rotate = int(Ncontrol.axis_x());
int rotatecw = int(Ncontrol.D_right());
int rotateccw = int(Ncontrol.D_left());
int shoulder = int(Ncontrol.axis_y());
int shoulderup = int(Ncontrol.D_up());
int shoulderdown = int(Ncontrol.D_down());
int elbowdown = int(Ncontrol.C_down());
int elbowup = int(Ncontrol.C_up());
int x2right = int(Ncontrol.C_right());
int x2left = int(Ncontrol.C_left());
int clawclose = int(Ncontrol.Z());
//Button Commands
if (rotate > 30){
myStepper.setSpeed(RPM);
myStepper.step(30);
}
else if (rotate < -30){
myStepper.setSpeed(RPM);
myStepper.step(-30);
}
else{ //rotate > -30 && rotate < 30
for (int i = 1; i < 5; i++){
digitalWrite(i, LOW);
}
}
if (clawclose == 1){
pos5 = pos5 +2;
}
if (shoulder < -30 | shoulderdown == 1 && pos1 > 0){
pos1 = pos1 - 2;
}
else if (shoulder > 30 | shoulderup == 1 && pos1 < 180){
pos1 = pos1 + 2;
}
//Serial print data
Serial.print("Z: ");
Serial.print(clawclose);
Serial.print("\t");
Serial.print("X-axis: ");
Serial.print(rotate);
Serial.print("\t");
Serial.print("pos: ");
Serial.print(pos1);
Serial.print("\t");
Serial.print("D_up: ");
Serial.print(shoulder);
Serial.print("\t");
Serial.print("C up: ");
// Serial.print(y2up);
Serial.print("\t");
Serial.print("D down: ");
Serial.print(shoulderdown);
Serial.print("\t");
Serial.println("D_right: ");
//Ncontrol.print_N64_status();
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.