I really don't know if this is the place to post this, but i'm quickly running out of options, and i was hoping you guys could help me with my Arduino project.
Here's the setup: I have a continuous rotation servo and a PIR sensor. What i want to do is basically rotate the servo a fixed amount whenever the sensor detects movement. Is this possible? Any previous attempts at creating a code for this have either made the servo rotate forever or not do anything at all. Can you guys help me out? Please :(?
Once one modifies a servo for continuous rotation it is no longer a servo. The ex-servo rather has morphed into becoming a bi-directional variable speed geared motor drive. So you have lost the ability to have it move to a specific point and stop.
You could put an arm on the shaft of the servo and have the arm trip some microswitches, or optisolators, use those to turn power on/off to the servo via transistor or relay control.
After some digging around on the internet i found a code that makes an LED blink when the PIR sensor detects motion. Is there any way to replace the LED blink by a Servo 1 second rotation to the right? Here is the code:
// example for the PIR motion sensor SE-10
int timer = 500;
int alarmPin = 0;
int alarmValue = 0;
int ledPin = 11;
void setup () {
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(alarmPin, INPUT);
delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can detect infrared presence.
}
void loop (){
alarmValue = analogRead(alarmPin);
if (alarmValue < 100){
blinky(); // blinks when the motion has been detected, just for confirmation.
}
delay(timer);
Serial.println (alarmValue);
delay (10);
}
void blinky() {
for(int i=0; i<3; i++) {
digitalWrite(11,HIGH);
delay(200);
digitalWrite(11,LOW);
delay(200);
}
}
I did not make the above, but it does work correctly when i hook it all up, so yeah. Sorry guys, i'm not a very good programmer >.<
When that code sees a low value from the PIR, it calls a function, blinky().
That function turns the LED on, waits, turns the LED off, and waits again, in a loop. It executes the loop 3 times.
Get rid of the loop, and change the time that the function waits after turning the LED on. Remove the wait after turning the LED off.
Rename the function, because it no longer blinks an LED, and change the pin to the one that the servo is attached to.
The function will then run the servo for the specified period of time every time the PIS sensor outputs a low value.
The next thing to consider is that maybe you don't want the function to be called EVERY time the sensor outputs a low value. Maybe you only want to call the function ONCE when the sensor outputs a low value. Think about how you might do that.
The next thing to consider is that maybe you don't want the function to be called EVERY time the sensor outputs a low value. Maybe you only want to call the function ONCE when the sensor outputs a low value. Think about how you might do that.
Hmm, some sort of activation condition? I don't know the exact term on the arduino programming, but what i mean by activation conditions is:
if (alarmValue < 18) & rotated = 0, it would rotate the servo to one side and add something like "rotated = 1"
By this logic the code could be expanded to rotate it to the other side as in:
if (alarmValue > 1023) & rotated = 1 it would rotate to the opposite side.
Where rotation() rotates the servo to one side and reverotation() rotates it to the opposite one, but im getting a ""rotated" was not declared in this scope" error when compiling, so im assuming i have to declare the flag beforehand. How would i accomplish this?
I apologize in advance for sounding so noobish, because i actually am a noob at this >.<
You are missing a set of ( ) around the condition, no ;needed.
if ( (alarmValue < 100) & (rotated = 0) ){
Check the reference page for the difference between & and && also.
One does what you want here, the other does a Logical And operation on the two elements.
Same with = and ==. One sets a value, the other does a comparison.
CrossRoads:
You are missing a set of ( ) around the condition, no ;needed.
if ( (alarmValue < 100) & (rotated = 0) ){
Check the reference page for the difference between & and && also.
One does what you want here, the other does a Logical And operation on the two elements.
Same with = and ==. One sets a value, the other does a comparison.
Alright, thanks! It's kinda hard to understand at first, but i think im getting the hang of it.
It's still giving me the "X was not declared in this scope" error though, so where do i "declare it", so to speak?
EDIT:
"rotated = 0" will always be false. Did you mean "rotated == 0"?
I meant to use it as a flag of sorts, it would be false UNTIL i declared it true, and vice-versa. So yeah, i meant ==, i think :S
Okay guys, i don't know if double posting is allowed here, but anyway:
The point of this code/setup was to make a door automatic. So basically what i wanted to do was open the door when the sensor detects movement, then close it when it stops detecting it.
Obviously i'd also need an idle condition, or else the door would just open and close forever. So i make some revisions to the code of which i am not so sure about and hook everything up.
The problem quickly becomes visible: the servo is rotating less than a milimeter every time the PIR sensor makes a pulse on the serial monitor. How do i fix this, and could someone explain what the problem is? I probably messed up the code. Again >.<
This code is assigning the value 0 to rotated, not comparing the value of rotated to 0.
byte rotated (0);
I'm not sure what this code does. Try:
byte rotated = 0;
byte rotated (1);
Declaring a local variable with the same name as a global variable is almost never a good idea. If this is one of those rare cases where it is, please explain why it is a good idea. Your local variable goes out of scope immediately, so it is hard to see the benefit of declaring the local variable.
This is NOT how to command a servo to move. There is a Servo library. Research it and use it.
the servo is rotating less than a milimeter
Please explain how something rotates in length units. Things typically rotate in angular measurements, like degrees. We have no idea how long your servo arm is, so telling us how far some point on the servo arm moved is a useless bit of information.
Please explain how something rotates in length units. Things typically rotate in angular measurements, like degrees. We have no idea how long your servo arm is, so telling us how far some point on the servo arm moved is a useless bit of information.
Well, the servo arm is about an inch long, and with every pulse it rotates about a degree to the left, if you're facing the arm.
Declaring a local variable with the same name as a global variable is almost never a good idea. If this is one of those rare cases where it is, please explain why it is a good idea. Your local variable goes out of scope immediately, so it is hard to see the benefit of declaring the local variable.
Well the basic idea of what i wanted to do(i sound like an idiot -.-) was to add a flag of sorts so the Arduino software knows whether the door is open or closed, if this is the wrong way to do it, i'd appreciate a heads up on how to actually do it.
This is NOT how to command a servo to move. There is a Servo library. Research it and use it.
On the library i can only find rotation codes for non continuous servos (e.g codes that rotate the servo a fixed amount of degrees either to the left or right) and so forth, could someone direct me to a library concerning continuous rotation servos?
On the library i can only find rotation codes for non continuous servos (e.g codes that rotate the servo a fixed amount of degrees either to the left or right) and so forth, could someone direct me to a library concerning continuous rotation servos?
Same library is used, same signal to the servo. However while a normal servo rotates and stops given a signal from 0 (degrees) to 180 ( degrees). The same commands going to a modified servo would result in 0 = full speed in one direction, 180 = full speed on the opposite direction, and 90 = stop. Values sent between 0-89 or 91-180 would result in continous rotatation at a specific speed. Note that the 90 degrees = stop may have to be tweeked to find the specific stop condition for your specific servo due to calibration variation between servos.