Ciao a tutti.
Dopo aver risolto grazie al vostro aiuto il problema di comunicazione con la scheda HC12 sono riuscito ad andare avanti con il mio progetto.
Adesso ho due oggetti:
- Trasmettitore = pulsantiera a 5 tasti;
- Ricevitore = controllo 5 servi.
Tutta la parte circuitale è corretta. Premendo i vari pulsanti sul trasmettitore i relativi servi collegati al ricevitore si spostano nella posizione voluta.
Solo che.... si mettono a ballare ritmicamente. Ogni servo in sequenza compie uno spostamento di alcuni gradi (ad occhio diciamo meno di 5°). Ascoltandoli si sente anche come una specie di ritmo.
Penso che sia una questione di codice in quanto l'alimentazione fornita è adeguata (6V), tutti i GND (Arduino, servi, batteria) sono connessi e, soprattutto, mettendo un codice diverso in cui i servi non sono comandati a distanza ma seguono una routine fissa (cambiano posizione tutti dopo 2 secondi) si muovono correttamente e poi stanno fermi in posizione.
Ho fatto varie ricerche e pensavo di aver trovato la soluzione visto che ad ogni iterazione ordinavo ai servi di raggiungere la posizione anche se erano già in quella voluta. Però anche dopo la correzione il problema persiste.
Quindi o non era quello il problema oppure non ho corretto nel modo giusto.
A livello di Hardware il ricevitore prevede una Arduino nano, una scheda HC12 e 5 servi SG90 9g.
Questo il codice nella sua ultima versione (scusate la sua bruttezza)
int PinOut[5] = {3, 5, 6, 9, 11}; // Servo pins
const int HCRX = 8; // the number of the Arduino TX pin
const int HCTX = 10; // the number of the Arduino RX pin
int buttonState = 0; // variable for reading the pushbutton status
int ServoState[5] = {50, 50, 50, 50, 50}; // Starting servos positions
#include <SoftwareSerial.h> // Includi la biblioteca Software Serial
SoftwareSerial HC12(HCRX, HCTX); // Create Software Serial Port
#include <Servo.h>
Servo myservo0; // create servo object to control a servo
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
void setup() {
Serial.begin(9600); // Open serial port to computer
HC12.begin(9600); // Open serial port to HC12
for (int i = 0; i < 5; i++) { // Output pins (Servo)
pinMode(PinOut[i], OUTPUT);
}
myservo0.attach(3); // attaches the servo on pin 3 to the servo object
myservo1.attach(5); // attaches the servo on pin 5 to the servo object
myservo2.attach(6); // attaches the servo on pin 6 to the servo object
myservo3.attach(9); // attaches the servo on pin 9 to the servo object
myservo4.attach(11); // attaches the servo on pin 11 to the servo object
myservo0.write(ServoState[0]); // sets the servo 0 to start position
myservo1.write(ServoState[1]); // sets the servo 1 to start position
myservo2.write(ServoState[2]); // sets the servo 2 to start position
myservo3.write(ServoState[3]); // sets the servo 3 to start position
myservo4.write(ServoState[4]); // sets the servo 4 to start position
}
void loop() {
while (HC12.available()) {
buttonState = HC12.read();
if (buttonState == 0 && ServoState[0] != 50) {
ServoState[0] = 50; // sets the servo 0 to position 0
myservo0.write(50);
}
if (buttonState == 1 && ServoState[0] != 120) {
ServoState[0] = 120; // sets the servo 0 to position 1
myservo0.write(120);
}
if (buttonState == 10 && ServoState[1] != 50) {
ServoState[1] = 50; // sets the servo 1 to position 0
myservo1.write(50);
}
if (buttonState == 11 && ServoState[1] != 120) {
ServoState[1] = 120; // sets the servo 1 to position 1
myservo1.write(120);
}
if (buttonState == 20 && ServoState[2] != 50) {
ServoState[2] = 50; // sets the servo 2 to position 0
myservo2.write(50);
}
if (buttonState == 21 && ServoState[2] != 120) {
ServoState[2] = 120; // sets the servo 2 to position 1
myservo2.write(120);
}
if (buttonState == 30 && ServoState[3] != 50) {
ServoState[3] = 50; // sets the servo 3 to position 0
myservo3.write(50);
}
if (buttonState == 31 && ServoState[3] != 120) {
ServoState[3] = 120; // sets the servo 3 to position 1
myservo3.write(120);
}
if (buttonState == 40 && ServoState[4] != 50) {
ServoState[4] = 50; // sets the servo 4 to position 0
myservo4.write(50);
}
if (buttonState == 41 && ServoState[4] != 120) {
ServoState[4] = 120; // sets the servo 4 to position 1
myservo4.write(120);
}
}
}
Grazie mille per l'aiuto.