Hello,
I have a problem with a while that I don’t see, when the motor stepper has made the 250 steps, it must stop and call the arm function, but when I press the button the two motors me to work.
I control the motor stepper with a timer.
void loop()
{
boolean etatBouton=digitalRead(Bouton_demarrage);
if(etatBouton == false && etat_moteur == false)
{
etat_moteur= true;
acceleration();
nb_pas= 0;
}
if(etat_moteur == true)
{
x = digitalRead(capteur_optique); // lit la valeur numérique du capteur_optique et met le résultat à x
if(x == true) // si l'état du capteur_optique est vrai
{
if(trait == true) // si l'état du capteur_optique est faux
{
etat_moteur= true;
valid_comptage= true;
}
trait= true;
}
else
{
trait= false;
}
Serial.println("avant"); //
while(nb_pas >= 250);
decceleration(); // deceleration here is problem
Serial.println("commande bras");
bras();// fonction arm //
}
}
I don’t understand why, when I press the button, both engines are running. What I want is I press the button, once the motor stepper made its 250 steps, the arm must go down.
int STEP = 6;
int DIR = 7;
unsigned int timer1_counter;
int Bouton_demarrage=9;
int etat_moteur = 0;
int x;
int capteur_optique = 10;
int trait= false;
int valid_comptage;
unsigned int nb_pas;
int E1 = 4;//4
int M1 = 5;//5
bras()
{
digitalWrite(E1,0);
analogWrite(M1,255);
if(analogRead(A10) >= 740)
{
digitalWrite(E1,0);
analogWrite(M1,0);
delay(2000);
while(analogRead(A10) >= 500)
{
digitalWrite(E1,1);
analogWrite(M1,255);
}
digitalWrite(E1,1);
analogWrite(M1,0);
}
}
tourne()
{
digitalWrite(DIR, 1); //direction/sens du moteur
digitalWrite(STEP, 1); //une impulsion a step
delayMicroseconds(2); //temps en microseconde
if(valid_comptage == true )
{
nb_pas++;// add a step
Serial.println(nb_pas);
}
digitalWrite(STEP, 0);
}
acceleration()
{
TIMSK1 |= (1 << TOIE1);
for(float vitesse = 0.2; vitesse<0.5;vitesse +=0.001)
{
float temps_tour = 1.0/vitesse;
float frequence = 100/temps_tour;
long compteur =(long )((16000000.0/(256*frequence))-1);
timer1_counter = 65536-compteur;
}
}
decceleration()
{
TIMSK1 |= (1 << TOIE1);
for(float vitesse = 0.5; vitesse>0.2;vitesse -=0.001)
{
float temps_tour = 1.0/vitesse;
float frequence = 100/temps_tour;
long compteur =(long )((16000000.0/(256*frequence))-1);
timer1_counter = 65536-compteur;
}
TIMSK1 &= (0<<TOIE1);
}
void setup()
{
Serial.begin(9600);
Serial.begin(9600);
pinMode(capteur_optique, INPUT),
pinMode(Bouton_demarrage,INPUT);
pinMode(E1, OUTPUT);
pinMode(M1, OUTPUT);
Serial.println("Démarrage");
digitalWrite(E1,1);
analogWrite(M1,255);
pinMode(A10, INPUT);
while(analogRead(A10) >=500)
{
}
digitalWrite(E1,1);
analogWrite(M1,0);
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
timer1_counter = 65536-312;
TCNT1 = timer1_counter;
TCCR1B |= (1 << CS12);
TIMSK1 &= (0<<TOIE1);
interrupts();
}
void loop()
{
boolean etatBouton=digitalRead(Bouton_demarrage);
if(etatBouton == false && etat_moteur == false)
{
etat_moteur= true;
acceleration();
nb_pas= 0;//sets the step to 0
}
if(etat_moteur == true)
{
x = digitalRead(capteur_optique); // lit la valeur numérique du capteur_optique et met le résultat à x
if(x == true) // si l'état du capteur_optique est vrai
{
if(trait == true) // si l'état du capteur_optique est faux
{
etat_moteur= true;// motor condition
valid_comptage= true;//valid count
}
trait= true;//feature
}
else
{
trait= false;//feature
}
Serial.println("avant");
while(nb_pas >= 250);//while less than 250 steps
decceleration(); // stop motor stepper
Serial.println("commande bras");
bras(); // fonction arm to move down
}
}
ISR(TIMER1_OVF_vect)
{
TCNT1 = timer1_counter;
if(etat_moteur == true)
{
tourne();// call under motor stepper program
}
}
Messir7:
I don’t understand why, when I press the button, both engines are running.
This is a very complex program to give rise to such a simple question. Did you write the program yourself?
If you did write it yourself then, presumably, there was an earlier version that works as required and the most recent changes are what is causing the problem. What are those changes?
You did not provide the explanation of the variables that I requested in Reply #1. And now that I have seen the whole program you will need to provide an explanation of what all the parts do. I am much too lazy to look up the Atmega datasheet to figure out all the register code.
If you did NOT write the program yourself then if you explain what you are trying to achieve it may be that a much simpler program would be sufficient.