Hey i have been trying to google this but i cant find out how to make delays and how to not start the motor depending on the state of the ir sensor. im using motor shields, and it works. but.
Before The Ontime Start, Like 40 seconds before. i want it to send a LOW signal to the soundpin 8. then wait 5 seconds
Then i want it to Check if the SensorPin is HIGH OR LOW. then wait 5 seconds.
If the sensor pin is LOW it will send a LOW to the mp3 card. And HIGH to The motor3 Pin.
if the Sensor Pin is High It will send a low to the mp3 card. and not start the motor and start a new timer for 15 seconds and the run the check again, when thats done it will restart the offTime timer wich is 70 sec.
Also i would like to make a timer to send a LOW to the mp3 card every hour or two. Maybe use the onTime1 and offTime1 for this? i tried but it didnt work, the music got squashed up.
// Currently Running on arduino mega 2560.
const unsigned int onTime = 3000; // The On Time of The motor Should be 3 Seconds.
const unsigned int onTime1 = 1; // Here i have tried to establish a interrupt signal to the mp3 card to Interupt so a sound will be played again.
const unsigned long offTime = 70000; // of time of the motor 70 Seconds.
const unsigned long offTime1 = 80000; // wait time of the intterupt signal.
const int PhotoPin = A0; // Pin that the photo resistor.
const int SensorPin = A1; // IR sensor pin here.
const int ledPin = 6; // a Led is connected here.
const int threshold = 700; // Threshold of the photopin.
const int SoundPin1 = 7; // The pin that sends a LOW to a mp3card if the photo pin is HIGH
const int SoundPin2 = 8; // The Pin that sends a LOW to the mp3 card if the motor is about to start.
const int SoundPin3 = 9; // The Pin that sends a LOW to the mp3 card if the ir sensor is LOW
const int MOTOR3 = 3; // Motor Pin.
// Tracks the last time event fired
unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
// Interval is how long we wait
int unsigned long interval = onTime;
int unsigned long interval1 = onTime1;
// Used to track if should be on or off
boolean MOTOR3state = true;
boolean SOUNDstate = true; // Here i tried to set sound state to the interrupt timer to the mp3card.
// Setup Stuff
void setup() {
pinMode (5, OUTPUT);
pinMode(MOTOR3, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(SoundPin1, OUTPUT);
pinMode(SoundPin2, OUTPUT);
pinMode(SoundPin3, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
//Set Pin 3 to state of motor state.
// If motor State hasn't changed, neither will the state of the motor/soundinterrupt.
digitalWrite(3, MOTOR3state);
digitalWrite(5, SOUNDstate);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
int analogValue = analogRead(PhotoPin);
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
if ((unsigned long)(currentMillis1 - previousMillis1) >= interval1) {
// Change wait interval, based on current motor state
if (MOTOR3state) {
if (SOUNDstate) {
}
// feedermotor is currently on, set time to stay off
interval = offTime;
interval1 = offTime1;
} else {
// motor is currently off, set time to stay on
interval = onTime;
interval1 = onTime1;
// Toggle The motor and interrupt sound.
MOTOR3state = !(MOTOR3state);
SOUNDstate = !(SOUNDstate);
// Save the current time to compare "later"
previousMillis = currentMillis;
previousMillis1 = currentMillis1;
}
// if the analog value is HIGH enough, send a signal to the sound card.
// And Start To Fade Led In and out.
if (analogValue > threshold) {
digitalWrite(SoundPin1, LOW);
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(40);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(40);
}
// If not make the ledPin stay HIGH and soundPin HIGH ( soundPin uses pullup on mp3 card)
} else {
digitalWrite(ledPin, HIGH);
digitalWrite (SoundPin1, HIGH);
}
}
}