Hey I am coming to you guys because I am in dire need of help. What I am trying to do is try and have a PIR Sensor and a Tilt Switch control two different motions on a dc motor in a RC Car. I am also trying to have two different sounds playing when each different motion is happening. Basically what I am trying to do is when someone walks by the PIR Sensor it plays a sound and the car moves forward. Also once this said person interacts with it and hits the tilt switch I want it to play another sound and move backward. I have two recordable sound cards hooked up to this two play the two independent sounds but the main issue is having both switches working together in the code. When I write the code for just one input, the sound, and the motor it works fine but its the combining of the two codes that isnt working for me. If anyone has an suggestions or help I would greatly appreciate your help. Thanks guys
Post your code. We aren't psychics here - we need to see what you're doing and/or have done already.
Sorry I apologize for not posting the code that was dumb of me. Here it is thanks for taking a look at it
#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ);
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
const int buttonPin = 10;
const int ledPin = 11;
const int ledPin2 = 13;
int buttonState = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Serial.println("Motor test!");
motor.setSpeed(200);
pinMode(analogInPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode (ledPin, OUTPUT);
pinMode (ledPin2, OUTPUT);
}
void loop() {
// read the analog in value:
int tmp;
tmp = analogRead(analogInPin);
sensorValue= (6787.0 /((float)tmp - 3.0)) - 4.0;
buttonState = digitalRead(buttonPin);
if (tmp < 3){
sensorValue= -1; // invalid value
}
else if (sensorValue <20){
digitalWrite(ledPin2, HIGH);
Serial.print("tock");
motor.run(BACKWARD);
}
else if (sensorValue >1200) {
digitalWrite(ledPin2, LOW);
Serial.print("tack");
motor.run(RELEASE);
}
if (buttonState == HIGH){
Serial.print("tack");
motor.run(RELEASE);
digitalWrite(ledPin, LOW);
}
else {
Serial.print("tick");
motor.run (FORWARD);
digitalWrite(ledPin, HIGH);
}
//else{
//sensorValue= (6787.0 /((float)tmp - 3.0)) - 4.0;
//}
// print the results to the serial monitor:
//Serial.print("sensor = " );
Serial.println(sensorValue);
delay(38);
}
Am I correct in assuming the 'potentiometer' is your tilt sensor and the 'button' is the PIR sensor? Otherwise I'm having some trouble figuring out how your code relates to your problem.
Yes the potentiometer is the PIR sensor and the tilt sensor is the button this is the way my design teacher told us to write the code. Is that wrong?
That's somewhat confusing. I'll get some example code for you to try up in ~10 minutes or so.
Thank you very much I appreciate it. Our design teacher told us to run the tilt sensor as if it were a button code and the PIR sensor if it was an analog input
Just a quick question before posting code:
If the car is not moving forward (PIR sensor not triggered) should the tilt switch be active, or should it be ignored?
I have a RC car with the PIR Sensor coming off of the side sensing the movement which makes it move forward, once there is no movement it stops. There is a metal rod coming off of the RC car with a balloon at the top. The tilt sensor is wired down from that balloon. Basically if someone hits the balloon the tilt switch will become active and make it move backward.
Its basically this car is trying to follow you from your movement but when you interact with it by punching or touch the balloon the switch will make it "run" away from you (move backward). I hope this makes sense.
This code has NOT been tested to compile.
What this code does is:
if (movement is detected from PIR AND tilt is not detected)
start moving forward
else if (movement is not detected AND tilt is not detected)
stop
else if (movement is not detected AND tilt is detected)
start moving in reverse
#include <AFMotor.h>
// motor driver
AF_DCMotor motor(2, MOTOR12_64KHZ);
// define inputs
#define PIR_IN_PIN 0 // PIR attached to analog pin 0
#define TILT_IN_PIN 10 // TILT attached to digital pin 10
// define outputs
#define LED_PIN_1 11 // LED1 attached to digital pin 11
#define LED_PIN_2 13 // LED2 attached to digital pin 13
enum state { ST_STOPPED, ST_FORWARD
, ST_REVERSE, ST_SOUND_1
, ST_SOUND_2 };
enum state current_state = ST_STOPPED;
void setup() {
// init serial at 115200 baud (no reason to be slow =))
Serial.begin(115200);
Serial.print("Motor Test:\r\n");
// setup inputs and outputs
// inputs
pinMode(PIR_IN_PIN, INPUT);
pinMode(TILT_IN_PIN, INPUT);
// outputs
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
digitalWrite(LED_PIN_1, LOW);
digitalWrite(LED_PIN_2, LOW);
}
int getPIRValue(uint8_t pin) {
return (6787.0 / (analogRead(pin) - 3.0)) - 4.0;
}
void loop() {
int sensorValue = getPIRValue(PIR_IN_PIN);
bool tilted = digitalRead(TILT_IN_PIN);
if (sensorValue < -4) {
// error state
}
if (sensorValue < 20 && !tilted) {
// movement
if (current_state != ST_FORWARD)
current_state = ST_SOUND_1; // sound first time
else
current_state = ST_FORWARD; // otherwise go forward
} else if (sensorValue > 1200) {
// no movement
if (tilted) {
// balloon is tilted
if (current_state != ST_REVERSE)
current_state = ST_SOUND_2; // sound first time
else
current_state = ST_REVERSE; // otherwise reverse
} else {
// balloon isn't tilted
current_state = ST_STOPPED;
}
}
switch (current_state) {
case ST_STOPPED:
Serial.print("MOTOR STOPPED\r\n");
digitalWrite(LED_PIN_1, LOW);
digitalWrite(LED_PIN_2, LOW);
motor.run(RELEASE);
break;
case ST_FORWARD:
Serial.print("MOTOR FORWARD\r\n");
motor.run(FORWARD);
break;
case ST_REVERSE:
Serial.print("MOTOR REVERSE\r\n");
motor.run(REVERSE);
break;
case ST_SOUND_1:
Serial.print("SOUND 1\r\n");
digitalWrite(LED_PIN_1, HIGH);
current_state = ST_FORWARD; // forward after sound
break;
case ST_SOUND_2:
Serial.print("SOUND 2\r\n");
digitalWrite(LED_PIN_2, HIGH);
current_state = ST_REVERSE; // reverse after sound
break;
}
}
--edit: added break statement in ST_SOUND_2
--edit2: replaced all references to buttons with references to tilts.
Thank you so much. I will not be able to test the code until tomorrow when I am back in the studio unfortunately my schools studio closes at 2:00 am I will post back letting you know how it worked out. This sounds like it is exactly what I am looking for. I appreciate it. Thanks again : )
No problem - I'm glad to help out. Be aware that you will have to replace the code in "ST_SOUND_1" and "ST_SOUND_2" in the switch/case to actually output sounds - right now they just light the LEDs until it reaches ST_STOPPED.