hi,
my goal is to control 4-5 servos with 1 Arduino Nano, what needs pwm.
for servos VarSpeedServo.h is included.
For first tests servos will be controlled for 2 positions (model railway rail switches) by buttons, where JC_Button.h is included.
The pins 5,6,9,10 (D-Side) work well for PWM, but 19,20 (A-Side) dont.
Is there something needed additionally to bring A-Pins to work as PWM-Output for Servos ?
my code `
//- weiche_test_schranke__vspeed4 09.06.2022
#include <VarSpeedServo.h>
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
#define FALSEVAL 0
#define TRUEVAL 1
#define STAT_OPEN 1
#define STAT_CLOSED 0
#define RESET 1
#define LED_RED 2
#define LED_YELLOW 3
#define LED_GREEN 4
#define SK_SPEED 5
#define MAXPOS 35
#define STARTPOS 145
#define STEPP 5
#define LOOP_DELAY 30
#define SHOW_DELAY 300
VarSpeedServo myservo1; // create servo object to control a servo
//VarSpeedServo myservo2; // create servo object to control a servo
//- gemaess INPUT_PULLUP - Definition ist 0=Event am Geber= True,
//- also 'umgedrehte' Logik-Werte , das wird dann halt hier durchgezogen..
//- nach Info von AZ-Delivery sind die PWM-faehigen PINS vom Ardi.Nano :
//- 5,6,9? - deswegen wurden die Geber (Reflexlichtschranken)
//- gegenueber Sketch-Version2 auf 7,8 gelegt !
int geber1 = 7; //- Umschalter LS/Taster
int geber2 = 8; //- Umschalter LS/Taster
Button myButton1(geber1);
Button myButton2(geber2);
int s_status = STAT_OPEN;
int val_g1 = FALSEVAL;
int val_g2 = FALSEVAL;
int alarm = FALSEVAL;
int g1_event = FALSEVAL;
int g2_event = FALSEVAL;
int count_g1 = 0;
int count_g2 = 0;
int pin_reset_btn = 12;
int val_reset_btn = FALSEVAL;
Button myButtonR(pin_reset_btn);
int pin_rail_switch_1 = 20;
void setup()
{
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(LED_RED, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
//- die Geber sind erstmal Taster, Nicht Schalter !
pinMode(geber1, INPUT_PULLUP); // sets the digital pin 7 as input
pinMode(geber2, INPUT_PULLUP); // sets the digital pin 8 as input
//pinMode(geber1, INPUT); // sets the digital pin 7 as input
//pinMode(geber2, INPUT); // sets the digital pin 8 as input
//pinMode(pin_reset_btn, INPUT_PULLUP); // sets the digital pin 12 as input
pinMode(pin_reset_btn, INPUT); // sets the digital pin 12 as input
myButtonR.begin();
myButton1.begin();
myButton2.begin();
myservo1.attach(pin_rail_switch_1);
//myservo2.attach(6);
myservo1.write(STARTPOS, 50, false);
//myservo2.write(STARTPOS, 50, false);
val_reset_btn = FALSEVAL;
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
void loop()
{
g1_event = FALSEVAL;
g2_event = FALSEVAL;
val_reset_btn = FALSEVAL;
show_vals("Values at Loop-Start");
val_reset_btn = myButtonR.read();
if(val_reset_btn == TRUEVAL)
{
Serial.println(" reset-btn clicked : all counts set to zero, status=OPEN ");
Serial.println("Val Reset-Btn :"+String(val_reset_btn));
count_g1 = 0;
count_g2 = 0;
val_reset_btn = FALSEVAL;
s_status = STAT_OPEN;
//- Weichen in Startpos fahren
myservo1.write(STARTPOS, 50, false);
//myservo2.write(STARTPOS, 50, false);
}
/*
else
{
Serial.println(" reset is off ");
}
*/
g1_event = myButton1.read();
if(g1_event == TRUEVAL)
{
weiche_abzwgd();
Serial.println("Val Btn1 clicked : "+String(g1_event));
}
g2_event = myButton2.read();
if(g2_event == TRUEVAL)
{
weiche_gerade();
Serial.println("Val Btn2 clicked : "+String(g2_event));
}
delay(LOOP_DELAY);
} //- end-loop
void weiche_gerade()
{
if (s_status == STAT_OPEN)
{
//Serial.println(" Weiche gerade !");
myservo1.write(MAXPOS, SK_SPEED, false); //- no wait, no blocking
//myservo2.write(MAXPOS, SK_SPEED, false);
Serial.println(" Weiche gerade ?");
s_status = STAT_CLOSED;
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
}
}
void weiche_abzwgd()
{
if (s_status == STAT_CLOSED)
{
//Serial.println(" weiche_abzwgd !");
myservo1.write(STARTPOS, SK_SPEED, false);
//myservo2.write(STARTPOS, SK_SPEED, false);
Serial.println(" Weiche abzweigd ?");
s_status = STAT_OPEN;
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
}
void show_vals(String text)
{
//- der print-Befehl ist Nicht identisch mit dem von Standard-C oder Standard-Java !
//- 2. Parameter ist hier für das Ausgabeformat, nicht für nen numerischen Wert zur Ausgabe !
Serial.println(text);
Serial.print(" g1_count : ");
Serial.print(count_g1);
Serial.print(" g2_count : ");
Serial.print(count_g2);
Serial.print(" Status : ");
Serial.print(s_status);
Serial.print(" alarm : ");
Serial.println(alarm);
delay(SHOW_DELAY);
}