Motor and sensor

Hi everyone my name is swaggydogy and iam here to ask for some advices...

Im on a current project with arduino witch i meet some issue.

Im using on this project an motor, a LED and a sensor.

The idea between this is that i want to make a machine like this:

Capture d’écran 2018-04-15 à 14.23.10.png

So i'll explain there is a circle platform(in dark blue) witch is paste to the top of the motor ( the white objet) all i want to make it rotate, turn the platform with motor.

But i want the motor to make 90° rotation...

So the sensor ( the blue one) will detect the red box and make a signal.
The red box are placed in the corner of the platform, next to the little circle, just to symbolize the 90° degry of the retation.

All i want is to the beggining, the motor is already on, the motor turn but when the sensor detect something (it's mean the red box) it will make the motor off, stop rotate.

Of course if nothing is detected yet, the motor will still rotate.

Here the image of the circuit, i have to tell that the circuit and also the code are combination of other circuit, code.

For the circuit, here are the models of circuit that I have used:

For the sensor:

For the motor:

Capture d’écran 2018-04-15 à 15.15.30.png

And here my circuit:


About the code here are the model's code:

for the sensor:

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
 
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
}
 
void loop()
{
  buttonState = digitalRead(buttonPin);//lecture du capteur
  if (buttonState == HIGH) //si quelquechose est detecte
  {
    digitalWrite(ledPin, HIGH); //on allume la LED
  }
  else //sinon
  {
    digitalWrite(ledPin, LOW); //on eteint la LED
  }
}

for the motor:

void setup() {
  pinMode(3, OUTPUT);
}

void loop() {
  for (int i = 0; i = 50; i++) {
    analogWrite(3, i);
    delay(10);
  }
  
}

And now here is my code for my machine:

const int buttonPin = 2; // brooch for sensor PIR
const int ledPin = 12; //  the LED Arduino
int buttonState = 0; // statut of exit of the sensor

void setup() {
 pinMode(3, OUTPUT);
 pinMode(ledPin, OUTPUT); //brooch of the LED out put
 pinMode(buttonPin, INPUT); //brooch of the sensor in enter
}

void loop() {
 for (int i = 0; i = 50; i++) // we turn the motor 
   {
   analogWrite(3, i);
   delay(10);
   }
 buttonState = digitalRead(buttonPin);// reading of the sensor
 if (buttonState == HIGH) // if something is detected
 {
   digitalWrite(ledPin, HIGH); // LED on
   for (int i = 50; i = 0; i++) // We stop the rotation of the motor
   {
   analogWrite(3, i);
   delay(10);
 }
 }
}

Here that all, however the final code ( the last one ) doesn't work properly, it does make the motor on but the detection of the sensor doesn't make the motor stop rotate...

Can you help me out?

Capture d’écran 2018-04-15 à 14.23.10.png

Capture d’écran 2018-04-15 à 15.15.30.png

You have posted your code but you have not told us what it actually does and what you want it to do that is different.

To make it easy for people to help you please use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

I get a 410 Page Expired on that link: rather attach the pic to the thread, not expect others to traipse off to other sites, especially if it doesn't work :wink:

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

i don't know how to put an image i try with ImgBB but it doesn't show up...

You have to attach an image to your post, then you can go and edit your post to have it show up. Yes it's cumbersome. See this image guide.
You should be able to use the same tags for externally hosted images, but you rather attach it to the post to make sure it continues to show in the posts as long as the Arduino site itself is alive.

Hi,
Make sure your image is less than 1M in size and/or about 750pixels wide and or high.

jpg or png.

Look at the help link about how to attach a file to your post.

Tom... :slight_smile:

ok i gonna remove this topic to met a new thank for the help about the image

Swaggydogy:
ok i gonna remove this topic to met a new thank for the help about the image

I don't understand what you mean, but please don't start another Thread about the same problem. Just continue with this one.

...R

but i need that people see a clean topic quickly...

Use a ir proximity sensor instead of pir the code will be very easy ur main motive s to stop the moto when u detect anything obstruction

Swaggydogy:
but i need that people see a clean topic quickly...

All you need for that is to post the new information in the next Reply - the one people will be reading next. You can always edit your Original Reply and add "Please Skip to Reply NN"

...R

You have added a lot of stuff in your Original Post which now makes several of the comments look ridiculous to a new reader.

You have not told us what sensor you are using - post a link to its datasheet.

Also,

  • what size is the machine?
  • What size are the red pillars that need to be detected?
  • How far from the pillars is the detector?
  • How fast will the pillars be rotating?

...R

first of all I really sorry Robin2, i have make all clear to the original topic and it's make all the other comment useless...

And about the sensor, well in the project im on... I use an IR sensor but in tinkercad with the circuit only the PIR sensor work similar to an regular IR sensor...

The code that i put to it was in origin, an programm that work for an IR sensor.

Also about the red pillar and the distance, it's doesn't matter, all you need is that it could detect.

About the speed of the motor.

I want to make a regular speed witch is not really fast to give the time for the sensor to detect and not to slow to rotate not too slow to the point.

And dear kishalay: The PIR sensor in the circuit was only to see if the code work with Tinkercad.
Tinkercad is an site witch is really good for make simulation for arduino, 3D models, and circuit.

In fact the code that i trying to make is a code the more simple ways witch it could work correctly in my project.

About the cricuit it's just a way to represent the circuit the system. Only the code it's important.

Swaggydogy:
first of all I really sorry Robin2, i have make all clear to the original topic and it's make all the other comment useless...

What is worse: I for one, subscribed to this topic already, DIDN'T EVEN NOTICE THIS until it was mentioned in the comments.

So you're providing new information, but quite a few of the people that actually try to offer help simply won't see it this way. I'm reading NEW replies which is where I expect to find new information, not going back in threads to see whether there happens to be changes to those posts.

OK now I've read the OP again, and it raises indeed more questions than it answers.
What is this sensor, really? Code suggests PIR sensor but those won't detect a metal pillar, they detect movement of warm objects such as people and other warm-blooded animals, cars, etc.
How do you stop (and hold) the motor and platform at the correct point? Just cutting power won't make it stop there and then, just slow down gradually until it comes to a stop.

Swaggydogy:
And about the sensor, well in the project im on... I use an IR sensor but in tinkercad with the circuit only the PIR sensor work similar to an regular IR sensor...

You need to do real tests with a real sensor. I would not trust simulations for something like you are doing. Then post the datasheet for the specific sensor you are using.

Also about the red pillar and the distance, it's doesn't matter, all you need is that it could detect.

That is plain silly (and I am trying to be polite). Some sensors only detect at distances of a few millimetres. And some detectors can only sense large objects.

About the speed of the motor.

I want to make a regular speed witch is not really fast to give the time for the sensor to detect and not to slow to rotate not too slow to the point.

Don't waffle. Tell us the speed in revolutions per minute. Computer programming needs numbers.

...R

Well im not on a real project...
Im a school and in our current project we just can't do a finish project.

If i come here it's to ask for some help about the code...
The simulation, the circuit...
All are here to show you how the system work.
But i need that someone see what make the system not working correctly about the code.

About the sensor...

I'll be clear, in reality it's an IR sensor and it detect if something is in the area that the sensor can detect.
And in the system in 3D...

The sensor will detect the red pillar and make a signal that can stop the motor rotating...
And the red pillar are placed to make the perfect 90° rotation.

No matter how far the sensor can detect, i just need it to detect and it will not affecte the code because the 3D models are only here to show how the machine works...

Just try to think that the sensor code it's in origin a code that worked for an IR sensor and in Tinkercad only ( i think) the PIR models work similar to an regular Ir sensor...

Just please, i would like to get some help about the code...

Swaggydogy:
Just please, i would like to get some help about the code...

I am assuming the code is what is in your Original Post. This is the code from loop()

void loop() {
 for (int i = 0; i = 50; i++) // we turn the motor
   {
   analogWrite(3, i);
   delay(10);
   }
 buttonState = digitalRead(buttonPin);// reading of the sensor
 if (buttonState == HIGH) // if something is detected
 {
   digitalWrite(ledPin, HIGH); // LED on
   for (int i = 50; i = 0; i++) // We stop the rotation of the motor
   {
   analogWrite(3, i);
   delay(10);
 }
 }
}

I suspect the main problem with that is your use of FOR because all the code in a FOR loop will complete before anything else happens. So, for example the speed of the motor increases in 50 steps over a period of 500 millisecs before there is any attempt to read the sensor. And then if the sensor is detected there will be another 50 steps ...

And as well as that you have the syntax for your FOR loops wrong. The first one should be

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

and the second one should be

for (int i = 50; i >= 0; i--) // We stop the rotation of the motor

However I have no idea how far the motor will move in 500 millisecs
And what do you think will happen if the sensor is not detected the first time?

A more sensible approach is to set the motor rotating with a single call to analogRead() analogWrite() and then repeatedly check the sensor. Something like

void loop() {
   if (motorMoving == false) {
      analogWrite(motorPin, pwmValue);
      motorMoving = true;
   }
   if (motorMoving == true) {
      sensorVal = digitalRead(sensorPin);
      if (sensorVal == HIGH) {
         analogWrite(motorPin, 0);
         motorMoving = false;
      }
   }
}

Have a look at these links
Several Things at a Time
Planning and Implementing a Program

...R

Hi you're code is interresting but i don't understand what you're saying about the analogRead()

What does it do exactly and in what in can help for my project in my code?

also i don't understand how you're code work...

I see that you're replace the int (i = 0; i=50; i++) by an fonction: motorMoving

But i don't know how you managed to replace it..

Did you put and constant at the beginning? Do i have to put a newfonction on the void loop?