Go Down

Topic: Motor and sensor (Read 747 times) previous topic - next topic

wvmarle

Code: [Select]

for (int i = 0; i = 0; i++) // we turn the motor

    for (int i = 0; i = 50; i++)

Neither for loop makes sense. You're also re-using variable i in the inner loop.

The second would probably needs to be something like:
Code: [Select]

    for (int j = 0; j < 50; j++)



The first... just doesn't make sense.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Swaggydogy

maybe but in try here to make a constant value fo speed for the motor

Robin2

But it make error because i think the:

Code: [Select]
for (int i = 0; i <= 50; i++)

doesn't work with the 'if'
Of course it won't. Why would you expect the code for a FOR loop to work with an IF statement.

Please put this project to one side for the moment and, as I suggested in Reply #22, take some time out to learn the basics of programming.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Swaggydogy

Hi everyone good news i edit the code and it's gave me no error.

Code: [Select]
const int buttonPin = 2; // broche du capteur PIR
const int ledPin = 12; // la LED du Arduino
int buttonState = 0; // etat de la sortie du capteur
int pinMoteur = 7; // broche pour le moteur

void setup()
{
 
  pinMode(ledPin, OUTPUT); //la broche de la LED est mise en sortie
  pinMode(buttonPin, INPUT); //la broche du capteur est mise en entree
  pinMode(pinMoteur,OUTPUT);
}
 
void loop()
{
  if (pinMoteur = LOW) // Si le moteur n'est pas lancè
  {
 
    digitalWrite(ledPin, LOW); // la LED est éteinte
   
    digitalWrite(pinMoteur,HIGH);//moteur se lance
    delay(10);
  }
 
  if (pinMoteur = HIGH) //si le moteur est lancé
  {
    buttonState = digitalRead(buttonPin);//lecture du capteur
    if (buttonState == HIGH)
  {
    digitalWrite(pinMoteur,LOW); //le moteur s'arrête
    delay(10);
    digitalWrite(ledPin, HIGH); //on allume la LED
  }
  } 
 }



But the motor don't rotate...

wvmarle

Your if statements are wrong. Use == instead of =. That also should probably be a digitalRead().

pinMoteur should contain your pin number, so you don't want to assign a new value to it (which is what you actually do in those if statements). This can also be made const, had you done so  the compiler would have complained. Also make those variables a byte, saves some memory.

Code: [Select]

const byte buttonPin = 2; // broche du capteur PIR
const byte ledPin = 12; // la LED du Arduino
byte buttonState = 0; // etat de la sortie du capteur
const byte pinMoteur = 7; // broche pour le moteur

void setup()
{
 
  pinMode(ledPin, OUTPUT); //la broche de la LED est mise en sortie
  pinMode(buttonPin, INPUT); //la broche du capteur est mise en entree
  pinMode(pinMoteur,OUTPUT);
}
 
void loop()
{
  if (digitalRead(pinMoteur) == LOW) // Si le moteur n'est pas lancè
  {
    digitalWrite(ledPin, LOW); // la LED est éteinte
    digitalWrite(pinMoteur,HIGH);//moteur se lance
    delay(10);
  }
 
  if (digitalRead(pinMoteur) == HIGH) //si le moteur est lancé
  {
    buttonState = digitalRead(buttonPin);//lecture du capteur
    if (buttonState == HIGH)
      {
      digitalWrite(pinMoteur,LOW); //le moteur s'arrête
      delay(10);
      digitalWrite(ledPin, HIGH); //on allume la LED
    }
  }
}
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Swaggydogy

dear wvmarle i can't thank you enought.

The simulation work and it's what i need.

And again i can't be enough grateful to you.

Just one last thing...


Can you tell me what the difference between digitalRead and digitalWrite?

And also what the difference about '=' and '=='?

wvmarle

One reads the status, teh other wri
Can you tell me what the difference between digitalRead and digitalWrite?
One reads the pin status, the other sets it. Tutorials on both commands on this site.

Quote
And also what the difference about '=' and '=='?
The first is an assignment, the second a boolean comparison. Read up on those, it's basic programming.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Swaggydogy

thank very much for you're help.  :)  :)  :)  :)

Go Up