Bonjour,
Je rencontre un problème d'affichage de la valeur AO dans le projet suivant:
Configuration: carte UNO + afficheur 2x16 caractères en I2C
Projet: commande de moteur pas à pas avec 3 boutons poussoirs pour le sens et l'arret + 2 autres poussoirs pour une commande impulsionnelle avant et arrière. La maquette fonctionne très bien et est identique à celle ci: Drive a large stepper motor with an arduino Part 1 - YouTube
Tout se complique lorsque je veux lire la valeur A0, la valeur du potentiometre s'affiche correctement mais les poussoirs deviennent inopérants et donc on ne peut plus démarrer le moteur.
Je remercie d'avance ceux qui me permettront d'avancer.
Le code est ci dessous:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int potValue = A0;
const int buttonPin = 12;//Jog Button clockwise
const int buttonPin1 = 11;//Jog Button Counter clockwise
int buttonState = 0;
#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8); // 9= step ,8= direction
AccelStepper stepper2(1, 9, 8); // 9= step ,8= direction
// Define our three input button pins
#define LEFT_PIN 3
#define STOP_PIN 13
#define RIGHT_PIN 10
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 10000
#define MIN_SPEED .01
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
lcd.init(); // initialisation de l'afficheur
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potValue)); // Prints value on PotValue to LCD
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (buttonPin,INPUT);
pinMode(buttonPin1,INPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
int potValue = analogRead(potValue);
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potValue)); // Prints value on PotValue to LCD
delay(10); // delay in between reads for stability
buttonState = digitalRead(buttonPin);
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 6000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
stepper1.runSpeed();
//Jog Button Clockwise
if (digitalRead(buttonPin) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(-current_speed);
stepper1.runSpeed();
}
//Jog Button Counter clockwise
if (digitalRead(buttonPin1) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(current_speed);
stepper1.runSpeed();
}
}
hello
en apparté, tu déclares 2 fois "potvalue"===>>int potValue = A0;
une fois en global (dans les déclarations)===>>int potValue
une foid en local (dans la loop)
que donne cette ligne dans la loop: int potValue = analogRead(potValue);
et celle qui suit, qu'affiche t'elle ?
lcd.print(analogRead(potValue)); // Prints value on PotValue to LCD
Bonjour, merci de ta réponse. J'ai fait des tas d'essais à partir de forums lus et j'ai surement des lignes inutiles qui trainent dans ce script. En fait je ne veux lire et afficher que la valeur A0 declarée au début mais il semble qu'il y ait un conflit dans le void loop entre l'affichage de AO et le script concernant la commande du moteur.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potValue = A0;
const int buttonPin = 12;//Jog Button clockwise
const int buttonPin1 = 11;//Jog Button Counter clockwise
int buttonState = 0;
#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8); // 9= step ,8= direction
AccelStepper stepper2(1, 9, 8); // 9= step ,8= direction
// Define our three input button pins
#define LEFT_PIN 3
#define STOP_PIN 13
#define RIGHT_PIN 10
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 10000
#define MIN_SPEED .01
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
lcd.init(); // initialisation de l'afficheur
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potValue)); // Prints value on PotValue to LCD
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (buttonPin,INPUT);
pinMode(buttonPin1,INPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
//int potValue = analogRead(potValue);
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.setCursor(0,1); // Sets the cursor to col 0 and row 0
lcd.print(analogRead(potValue)); // Prints value on PotValue to LCD
delay(10); // delay in between reads for stability
buttonState = digitalRead(buttonPin);
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 6000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
stepper1.runSpeed();
//Jog Button Clockwise
if (digitalRead(buttonPin) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(-current_speed);
stepper1.runSpeed();
}
//Jog Button Counter clockwise
if (digitalRead(buttonPin1) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(current_speed);
stepper1.runSpeed();
}
}
Merci infobarquee et pepe pour vos réponses. J'ai testé le script envoyé la valeur s'affiche et varie bien en fonction du potar mais les commandes moteurs sont toujours inactives. Pour activer les commandes moteurs il faut dans loop désactiver toutes les commandes d'affichage comme ci dessous:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potPin = A0;
const int buttonPin = 12;//Jog Button clockwise
const int buttonPin1 = 11;//Jog Button Counter clockwise
int buttonState = 0;
#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8); // 9= step ,8= direction
AccelStepper stepper2(1, 9, 8); // 9= step ,8= direction
// Define our three input button pins
#define LEFT_PIN 3
#define STOP_PIN 13
#define RIGHT_PIN 10
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 10000
#define MIN_SPEED .01
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
lcd.init(); // initialisation de l'afficheur
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin)); // Prints value on PotValue to LCD
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (buttonPin,INPUT);
pinMode(buttonPin1,INPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
//int potValue = analogRead(potPin);
//lcd.backlight();
//lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
//lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
//lcd.setCursor(0,1); // Sets the cursor to col 0 and row 0
//lcd.print(analogRead(potValue)); // Prints value on PotValue to LCD
//delay(10); // delay in between reads for stability
buttonState = digitalRead(buttonPin);
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 6000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
stepper1.runSpeed();
//Jog Button Clockwise
if (digitalRead(buttonPin) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(-current_speed);
stepper1.runSpeed();
}
//Jog Button Counter clockwise
if (digitalRead(buttonPin1) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(current_speed);
stepper1.runSpeed();
}
}
Je suis bien d'accord je ne comprends pas pourquoi le LCD perturbe le script qui se compile bien par ailleurs. Etant debutant je me suis contenté de reprendre l'intégralité du script de Mike Ivison qui fonctionne très bien et essayé de rajouter l'afficheur pour lire la valeur du potar. L'afficheur aurait été la cerise sur le gateau pour régler précisément la vitesse du moteur mon projet étant le pilotage d'une monture de telescope.
Merci de ta contribution.
teste ca
surtout que potValue ne sert a rien dans le code non plus
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potPin = A0;
const int buttonPin = 12;//Jog Button clockwise
const int buttonPin1 = 11;//Jog Button Counter clockwise
int buttonState = 0;
#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8); // 9= step ,8= direction
AccelStepper stepper2(1, 9, 8); // 9= step ,8= direction
// Define our three input button pins
#define LEFT_PIN 3
#define STOP_PIN 13
#define RIGHT_PIN 10
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 10000
#define MIN_SPEED .01
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
lcd.init(); // initialisation de l'afficheur
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin)); // Prints value on PotValue to LCD
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (buttonPin,INPUT);
pinMode(buttonPin1,INPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
affichage();
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 6000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
stepper1.runSpeed();
//Jog Button Clockwise
if (digitalRead(buttonPin) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(-current_speed);
stepper1.runSpeed();
}
//Jog Button Counter clockwise
if (digitalRead(buttonPin1) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(current_speed);
stepper1.runSpeed();
}
}
void affichage(){
lcd.clear();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.setCursor(0,1); // Sets the cursor to col 0 and row 0
lcd.print(analogRead(potPin)); // Prints value on PotValue to LCD
}
pour le script ci dessus plus de commande moteur et l'affichage commence par SensorVal: avec des caractères dont la luminosité diminue sans valeur affichée.J'ai bien noté le transfert de la partie affichage à la fin de Loop mais j'avais essayé sans aucun résultat. Merci tout de meme.
sartri81:
pour le script ci dessus plus de commande moteur et l'affichage commence par SensorVal: avec des caractères dont la luminosité diminue sans valeur affichée.J'ai bien noté le transfert de la partie affichage à la fin de Loop mais j'avais essayé sans aucun résultat. Merci tout de meme.
la luminosité diminue en lisant analogRead(potPin) ?
ca sent, soit un mauvais contact, branchement, alim insuffisante.
donne le lien du code, photo du montage, alim
j'ai aidé un pote pour faire son focuser et ca fonctionne très bien avec plus de choses que ce que tu n'as.
sans rien changer au montage avec mon script habituel et à condition de désactiver la partie affichage de loop, le moteur(14HS11) fonctionne bien à toutes les vitesses et l'afficheur reste allumé avec une valeur quelconque. La maquette est alimenté par le port USB de mon portable et le moteur est commandé par un EasyDriver 4.5. Le script est celui de mon post #5
Ce qui est incomprehensible est que cette maquette fonctionne très bien sans afficheur et que la partie afficheur dans Loop bloque le système. Il y a un truc dans les commandes de la partie Loop qui est incompatible avec l'affichage. Dans la video de Mike Ivison de mon 1er post il explique qu'il a eu du mal avec le code des jog button(commandes impulsionnelles) mais c'est justement ce qui m'a séduit car ces commandes permettent en astronomie de faire de petites corrections de trajectoire et ça je ne l'ai vue nul part sur le net.
mais c'est justement ce qui m'a séduit car ces commandes permettent en astronomie de faire de petites corrections de trajectoire et ça je ne l'ai vue nul part sur le net.
ben si, ca existe, faisant de l'astro aussi, il y a un prog qui existe depuis un moment et utilise un rasp 3 en parralèlle avec un autre soft qui permet de suivre et faire les corrections automatiquement.
iAstroHub3.0
Oui tout à fait mais de ce que j'ai compris ce système est utilisable pour des instruments avec monture du commerce, je suis avec une monture personnelle de plus de 200Kg.
Dans le dernier script envoyé avec la partie affichage à la fin du Loop il faut rajouter delay(100) pour avoir l'affichage correct avec la valeur du potar qui s'affiche et qui est réglable. Les commandes moteurs ne fonctionnent pas.
Ci dessous le code qui fonctionne( enfin Presque bien)
la partie affichage dans Loop doit etre après les premières conditions et avant les conditions Jog Button. Je ne sais pas pourquoi mais c'est ainsi, par contre si la vitesse est réglable et affichable le moteur marque un accoup à chaque tour. Ce problem disparait si on efface la partie affichage dans loop.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potPin = A0;
const int buttonPin = 12;//Jog Button clockwise
const int buttonPin1 = 11;//Jog Button Counter clockwise
int buttonState = 0;
#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8); // 9= step ,8= direction
AccelStepper stepper2(1, 9, 8); // 9= step ,8= direction
// Define our three input button pins
#define LEFT_PIN 3
#define STOP_PIN 13
#define RIGHT_PIN 10
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 5000
#define MIN_SPEED .01
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
lcd.init(); // initialisation de l'afficheur
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin)); // Prints value on PotValue to LCD
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(5000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (buttonPin,INPUT);
pinMode(buttonPin1,INPUT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop(){
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 6000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
lcd.clear();
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("SensorVal: "); // Prints Sensor Val: to LCD
lcd.setCursor(0,1); // Sets the cursor to col 0 and row 0
lcd.print(analogRead(potPin));
}
stepper1.runSpeed();
//Jog Button Clockwise
if (digitalRead(buttonPin) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(-current_speed);
stepper1.runSpeed();
}
//Jog Button Counter clockwise
if (digitalRead(buttonPin1) == HIGH){
current_speed = ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
stepper1.setSpeed(current_speed);
stepper1.runSpeed();
}
}