Rigth Before The Timer goes ON Start a Process.

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);
      }
    }
  }

I think you are trying to do too much all at once. Start with just one part of your project. For example, leave out everything except the SensorPin and making something happen (perhaps light an LED) 5 seconds after the pin changes from HIGH to LOW. When that works try another part of the project. When you can do each part on its own it will be time to combine them - again, step by step and not all at once.

Have a look at Planning and Implementing a Program - especially how it breaks the program into separate functions that can each be tested on its own.

...R

i have tested them all seperately, and it works. im only having some trouble with what i have in the post. i really just need to know how to start a process 40 seconds before the motor starts.

Why ask for advice if you are not going to follow it?

i have tested the code, and it works, The sensor pin can ligth up a led if i implant the program to do that. Same with the photopin, the only thing im having trouble with is to make that code before the motor starts.

If you want something to happen before something ekse happens, you need to turn it around. Something happens at N and next thing happend at N + 40.

You also only need one currentMillis. If you look at the click at a certain time and look st the clock a fraction of s second later, the time will be the same.

sterretje:
If you want something to happen before something ekse happens, you need to turn it around. Something happens at N and next thing happend at N + 40.

You also only need one currentMillis. If you look at the click at a certain time and look st the clock a fraction of s second later, the time will be the same.

can you set an example how to do this in the code? i dont really need the 40 seconds before the motor toggle. just that its running the code i wanted it to do in the first pose before it toggles on or off with the waiting time before it runs the code again. Untill it finishes.

Here i have made the code a little shorter so i implement the other code after.

// Currently Running on arduino mega 2560. 


const unsigned int onTime = 3000; // The On Time of The motor Should be 3 Seconds.
const unsigned long offTime = 70000; // of time of the motor 70 Seconds.
const int SensorPin = A1;   // IR sensor pin here.
const int ledPin = 6;       // a Led is connected here.
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;


// Interval is how long we wait
int unsigned long interval = onTime;


// Used to track if should be on or off
boolean MOTOR3state = true;


// Setup Stuff
void setup() {

  pinMode(MOTOR3, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(SoundPin1, OUTPUT);
  pinMode(SoundPin2, OUTPUT);
  pinMode(SoundPin3, 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);

  // 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();

  // Compare to previous capture to see if enough time has passed
  if ((unsigned long)(currentMillis - previousMillis) >= interval) {

      // Change wait interval, based on current motor state
      if (MOTOR3state) {
   
        }
        // feedermotor is currently on, set time to stay off
        interval = offTime;
       

      } else {
        // motor is currently off, set time to stay on
        interval = onTime;
        

        // Toggle The motor State. 
        MOTOR3state = !(MOTOR3state);
       


        // Save the current time to compare "later"
        previousMillis = currentMillis;

         }
}

woldsondre:
i have tested them all seperately, and it works. im only having some trouble with what i have in the post. i really just need to know how to start a process 40 seconds before the motor starts.

Have you carefully studied the code in the link I gave you?

A large part of it is to do with timing things using millis()

It may also give you some ideas about how to combine the different parts of your project.

...R

This does not do what you want it to do but demonstrates the principle of T-40 and T; declarations are a stripped down version of yours.

// pins
const int SoundPin1 = 7;  // The pin that sends a LOW to a mp3card if the photo pin is HIGH
const int MOTOR3 = 3;     // Motor Pin.

// motor and sound state
bool motorState = true;   // default motor on ??
bool soundState = true;   // default sound on ??

// timing constants
const unsigned int motorOntime = 3000;      // The On Time of The motor Should be 3 Seconds.
const unsigned long motorOfftime = 70000;   // of time of the motor 70 Seconds.
const unsigned long preroll = 40000;        // preroll time (before motor turns on)

// timing variables
unsigned long currentTime;
unsigned long motorLasttime = 0;
unsigned long motorInterval = 0;

void setup()
{
  ...
  ...
}

void loop()
{
  // get current time
  currentTime = millis();

  // if motor is off
  if(motorState == false)
  {
    if(currentTime - motorLasttime >= (motorOfftime - preroll))
    {
      digitalWrite(SoundPin1, HIGH);
    }
    
    if(currentTime - motorLasttime >= motorOfftime)
    {
      motorLasttime = currentTime;
      motorState = !motorState;
      digitalWrite(MOTOR3, motorState);
    }
  }
  // if motor is on (obviously)
  else
  {
    if(currentTime - motorLasttime >= motorOntime)
    {
      motorLasttime = currentTime;
      motorState = !motorState;
      digitalWrite(MOTOR3, motorState);
      digitalWrite(SoundPin1, LOW);
    }
  }
}

Notes
1)
Having a pin MOTOR3 implies that there are at least three motors. As I doubt that is the case, just call it MotorPin.
2)
Names with numbers don't mean anything; in above I used e.g. motorOntime so it's clear it's about the motor. You can do something similar that currently use xxx1.

A better approach might be to use a statemachine using a switch/case. I will see if I can write a little framework later on.