I am new. I have had my starter kit for less than 48 hours. Someone helped me with my base code, which I am now modifying. I can't determine why the servo engages with the tilt switch off. I thought it was set to only do so with the tilt switch in the on position.
Link to Tinkercad: Login | Tinkercad
Any thoughts?
#include <Servo.h>
int pos = 0;
Servo servo_9;
int DCmotor = 10;
int tiltSensor = 8;
int tilt = 0;
// Variables will change:
int ledState1 = LOW;
int ledState2 = HIGH;
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 500; // interval at which to blink (milliseconds)
void setup()
{
Serial.begin(9600);
servo_9.attach(9);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(DCmotor, OUTPUT);
pinMode(tiltSensor, INPUT);
servo_9.write(90); // Rest position
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LEDs
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW) {
ledState1 = HIGH;
ledState2 = LOW;
} else {
ledState1 = LOW;
ledState2 = HIGH;
}
// set the LED with the ledState of the variable:
digitalWrite(11, ledState1);
digitalWrite(12, ledState2);
}
tilt = digitalRead(tiltSensor);
if (tilt) {
digitalWrite(DCmotor, HIGH);
pos = random(181);
Serial.print("Position: ");
Serial.println(pos);
servo_9.write(pos);
delay(10);
}
else {
digitalWrite(DCmotor, LOW);
servo_9.write(90); // Rest position
}
}
What do you mean by "the servo engages"?
Every time round loop() the servo will always go either to a random position or to 90. What is it supposed to do?
Steve
I want the servo to remain completely at rest unless and until the tilt switch is in the "on" position.
Unfortunately "at rest" doesn't mean a lot either.
I'll try to guess what you really want. Maybe have a look at the StateChangeDetection example in the IDE. Then you can move the servo somewhere when the switch BECOMES on and stop writing to the servo when the switch BECOMES off. When you stop writing to the servo it stays where it was last sent which sounds like "at rest" to me.
Steve
Trust me: I've been speaking English for over 40 years. When an object which has the capability of moving is expected not to do so, it's easy to understand the description of being "at rest". It either moves or it doesn't. I needed it not to move under certain circumstances.
I'm using a different approach altogether, so please don't worry about splitting any more imaginary hairs.
Edit: I suppose it's easy to wrack up 5K posts with condescending non-answers such as this.
Good job with code tags on your first post.
pinMode(tiltSensor, INPUT);
tilt = digitalRead(tiltSensor);
Please explain more about the tilt sensor and how it is wired.
Is there a pull up or pull down resistor? What keeps the tilt sensor from floating (i.e. undefined state) when it is not tilted?
Hi,
Your tilt switch is either ON or OFF.
It does not sense motion as such but if the sensor is tilted passed a certain angle.
I read your code that;
If the tilt is ON/HIGH , then the servo goes to a random position and the motor output is active.
If the tilt is OFF/LOW , then the servo goes to 90deg position and the motor output is inactive.
How have you got your tilt switch wired?
Your code infers it is connected between the digital input pin and 5V, do you have a 10k pull down resistor between the digital input and gnd?
How do you drive your motor from the output pin?
What is the motor you are driving?
Have you replaced the tilt switch with a normal switch or a piece of wire to manually turn the input on and off?
Please post your schematic here and not on TinkerCad.
You should be able to EXPORT share, snapshot your circuit as a jpg and attach it to your next post.
Thanks.. Tom.. 