Salve, come ho scritto sul titolo, il mio semplice progetto con arduino uno R4 Wifi, sarebbe quello di far ruotare un servo motore parallax 360° , avanti e indietro,
questo il mio setup
I programmi si devono copiare ed incollare e poi racchiudere nei tag CODE ... in pratica seleziona la parte di codice e poi premi l'icona <code/> nella barra degli strumenti per contrassegnarla come codice.
questo è il primo sketch, dimentica quello che scritto prima, ne devo fare uno di sketch, questo sketch funziona, con un pulsante, il servo gira da 0 a 180 e viceversa, però volevo fare il merge dei due sketches che ho postato, in pratica far apparire la scritta Closed quando il servo ruota a 0 a 180 :
# include <Servo.h>
Servo myServo;
const int SwitchPin = 8;
byte butLst;
int butCnt;
void setup ()
{
Serial.begin (9600);
pinMode (SwitchPin, INPUT_PULLUP);
butLst = digitalRead (SwitchPin);
myServo.attach (9);
}
void loop ()
{
byte but = digitalRead (SwitchPin);
if (butLst != but) { // change
butLst = but;
delay (20); // debounce
if (LOW == but) { // press
butCnt++;
Serial.println (butCnt);
if (butCnt % 2)
myServo.write (180); // odd
else
myServo.write (0); // even
}
}
}
#include "Arduino_LED_Matrix.h" //Include the LED_Matrix library
//https://ledmatrix-editor.arduino.cc/
#include "closed_animation_sliding.h"
ArduinoLEDMatrix matrix;
void setup() {
matrix.begin();
}
void showFrame() {
static uint32_t timeRef = millis();
static uint16_t interval = 0; // intervallo di tempo tra un frame e il successivo
static uint8_t frameIdx = 0; // indice del frame 0÷45
if (millis() - timeRef >= interval) {
interval = 300; // adesso vale 300
timeRef = millis();
// esegue il 45 esimo frame e dopo frameIdx vale 46
matrix.loadFrame(closed_animation_sliding[frameIdx++]);
// riporta frameIdx a 0
frameIdx = frameIdx % 46;
}
}
void loop() {
showFrame();
}
Intanto prova quello sopra se funziona correttamente.
Se funziona, adesso è non "bloccante" e ti dovrebbe venire più semplice
integrare l'altro sketch a questo non "bloccante".
:\Users\massi\AppData\Local\Temp\.arduinoIDE-unsaved2025327-13324-tc1thl.35i1\sketch_apr27a\sketch_apr27a.ino: In function 'void showFrame()':
C:\Users\massi\AppData\Local\Temp\.arduinoIDE-unsaved2025327-13324-tc1thl.35i1\sketch_apr27a\sketch_apr27a.ino:14:12: error: 'uin16_t' does not name a type; did you mean 'uint16_t'?
static uin16_t interval = 0; // intervallo di tempo tra un frame e il successivo
^~~~~~~
uint16_t
C:\Users\massi\AppData\Local\Temp\.arduinoIDE-unsaved2025327-13324-tc1thl.35i1\sketch_apr27a\sketch_apr27a.ino:16:31: error: 'interval' was not declared in this scope
if (millis() - timeRef >= interval) {
^~~~~~~~
C:\Users\massi\AppData\Local\Temp\.arduinoIDE-unsaved2025327-13324-tc1thl.35i1\sketch_apr27a\sketch_apr27a.ino:16:31: note: suggested alternative: 'Serial'
if (millis() - timeRef >= interval) {
^~~~~~~~
Serial
exit status 1
Compilation error: 'uin16_t' does not name a type; did you mean 'uint16_t'?
Ti consiglierei di sforzarti un minimo: almeno prova a leggerei cosa ti segnala il computer. Il compilatore ti sta chiaramente comunicando che hai sbagliato a dichiarare un tipo di dato. Hai scritto "uin16_t" per la variabile "interval", ma la dichiarazione corretta è "uint16_t".
No, cioè io non conosco affatto la UNO R4, né tanto meno le librerie che stai usando.
Posso dirti solo che interval può essere un array di 45 elementi di tipo uint16_t, in questo modo per ogni frame puoi specificare il tempo.
in pratica vi mostro il progetto che sto realizzando, per adesso sono riuscito a ottimizzare il codice per l'uso tramite uno slider via cloud di arduino, adesso ci devo aggiungere la scritta sul diplay led matrix closed quando il servo si aziona da 0 a 180°.
di seguito i due sketches che dovrei unire in uno che funzioni quando il servo ruota da 0 a 180°, ho provato a modificare ma gli errori sono molteplici e ancora non riesco a risolverli.
#include <Servo.h>
#include <Arduino_LED_Matrix.h>
#include "closed_animation_sliding.h"
ArduinoLEDMatrix matrix;
Servo myServo;
const int SwitchPin = 8;
byte butLst;
int butCnt;
void setup ()
{
Serial.begin (9600);
pinMode (SwitchPin, INPUT_PULLUP);
butLst = digitalRead (SwitchPin);
myServo.attach (9);
matrix.begin();
}
void showFrame() {
static uint32_t timeRef = millis();
static uint16_t interval = 0; // intervallo di tempo tra un frame e il successivo
static uint8_t frameIdx = 0; // indice del frame 0÷45
if (millis() - timeRef >= interval) {
interval = 300; // adesso vale 300
timeRef = millis();
// esegue il 45 esimo frame e dopo frameIdx vale 46
matrix.loadFrame(closed_animation_sliding[frameIdx++]);
// riporta frameIdx a 0
frameIdx = frameIdx % 46;
}
}
void loop ()
{
byte but = digitalRead (SwitchPin);
if (butLst != but) { // change
butLst = but;
delay (20); // debounce
if (LOW == but) { // press
butCnt++;
Serial.println (butCnt);
if (butCnt % 2)
myServo.write (180); // odd
showFrame();
} else
myServo.write (0); // even
}
}
}
'''
C:\Users\massi\Documents\Arduino\sketch_apr26efunziona_copy_20250428160845\sketch_apr26efunziona_copy_20250428160845.ino:54:1: error: expected declaration before '}' token
}
^
Più di una libreria trovata per "Servo.h"
Usata: C:\Users\massi\Documents\Arduino\libraries\Servo
Non usata: C:\Users\massi\AppData\Local\Arduino15\libraries\Servo
exit status 1
Compilation error: expected declaration before '}' token
ho provato il merge, mi dà questo errore nella compilazione, non riesco a decifrarlo ;
#include <Servo.h>
#include <Arduino_LED_Matrix.h>
#include "closed_animation_sliding.h"
ArduinoLEDMatrix matrix;
Servo myServo;
const int SwitchPin = 8;
byte butLst;
int butCnt;
void setup ()
{
Serial.begin (9600);
pinMode (SwitchPin, INPUT_PULLUP);
butLst = digitalRead (SwitchPin);
myServo.attach (9);
matrix.begin();
}
void showFrame() {
static uint32_t timeRef = millis();
static uint16_t interval = 0; // intervallo di tempo tra un frame e il successivo
static uint8_t frameIdx = 0; // indice del frame 0÷45
if (millis() - timeRef >= interval) {
interval = 300; // adesso vale 300
timeRef = millis();
// esegue il 45 esimo frame e dopo frameIdx vale 46
matrix.loadFrame(closed_animation_sliding[frameIdx++]);
// riporta frameIdx a 0
frameIdx = frameIdx % 46;
}
}
void loop ()
{
byte but = digitalRead (SwitchPin);
if (butLst != but) { // change
butLst = but;
delay (20); // debounce
showFrame();
if (LOW == but) { // press
butCnt++;
Serial.println (butCnt);
if (butCnt % 2)
myServo.write (180); // odd
} else
myServo.write (0); // even
}
}
questo è lo psuedo merge, ma non dà il risultato sperato, il servo non si muove e la scritta si muove un frame alla volta quando premo il pulsante, frizzata.