using t.oscillate - How to change the on time of a motor

Hello all. I'm new to this forum and this is my first post.

I would really appreciate some guidance. I have a code that changes the brightness of an LED with each press of a single button. What I would like to do is change the on time of 2 oscillating pumps at the same time the LED changes state. I have 2 oscillating water pumps in and Aquarium that are controlled by t.oscillate. There are 2 buttons. The button function works fine until the pumps start oscillating. On the first press of button1, the LED and pumps turn off. On the second press of button1, the LED and pumps turn on. On the first press of button2, the LED turns on dim. On the second press of button2, the LED gets a bit brighter and the pumps start oscillating back and forth at 2000ms intervals. On the third press of button2, the LED gets a bit brighter but the pumps stay at the 2000ms rate. From this point on the pumps never change from this 2000ms oscillation. I'm trying to get them to increase to 4000ms on that third press of button2.

I will post the part of the code that is giving me problems.

I have tried many things to remedy this to no avail. Any help would be greatly appreciated. This is my first post on this site so please forgive my errors.

void resolveOutputs() {

  switch (LEDState) {
  
    case 0:

      digitalWrite(outputPin, LOW);

      digitalWrite(pump1, LOW);
      digitalWrite(pump2, LOW);

      break;

    case 1:

      digitalWrite(outputPin, HIGH);

      digitalWrite(pump1, HIGH);
      digitalWrite(pump2, HIGH);

      break;

    case 2:
  

      analogWrite(outputPin, 30);


      break;

    case 3:

      analogWrite(outputPin, 70);

  
      t.oscillate(pump1, 2000, LOW);    
      t.every(1000, takeReading);

      t.oscillate(pump2, 2000, HIGH);
      t.every(1000, takeReading);
    
      break;
     

    case 4:

      analogWrite(outputPin, 100);

            t.oscillate(pump1, 4000, LOW);    
      t.every(1000, takeReading);

      t.oscillate(pump2, 4000, HIGH);
      t.every(1000, takeReading);

      break;

    case 5:

      analogWrite(outputPin, 155);

      break;

    case 6:

      analogWrite(outputPin, 255);

      break;

    default:

      break;

  }

}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Please post your complete code.
Where did you get the “t.oscillate” library and what is it supposed to do?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
In particular how have you got your buttons wired?

What model Arduino are you using?
What are you using to power your project?
What is an oscillating pump, link to specs/data will help?

Thanks… Tom… :slight_smile:

I will post the part of the code that is giving me problems.

I will post the part of the answer that is giving me problems.

Well, there was no part of the answer that gave me problems.

Post ALL of your code. Only YOU know what the heck a t is.

Thank you guys for taking the time to read my post. I don’t understand why you are not seeing the code that I posted. I’m doing some more digging on my own and need to step away from this code for a couple days to come back fresh. I will make some changes and get back with you. Thanks again guys.

frotown: Thank you guys for taking the time to read my post. I don’t understand why you are not seeing the code that I posted. I’m doing some more digging on my own and need to step away from this code for a couple days to come back fresh. I will make some changes and get back with you. Thanks again guys.

Because the code you posted begins:

void resolveOutputs() {

Where is the rest of it?

Paul

hellow again,

I have spent several more hours and have change my code and have fixed many of the problems.

This is an aquarium auto top off, which consist of two float switches and a solenoid valve which opens to let water into the tank when the level is low.

And a wave maker which oscillates 2 water pumps back-and-forth.

This code is 3 codes that I have smashed together. So it’s a bit of a mess but I’m doing the best I can I promise.

When the Arduino powers up the water pumps oscillate back-and-forth in 1 second intervals. When I click button 1 both pumps go off, and when I click it again both pumps come on which is working normally.

When I click the 2nd button both pumps oscillate normally back-and-forth in 1 second intervals. When I press the 2nd button again both pumps oscillate back-and-forth in 4 second intervals, which is correct.

When I push button to a 3rd time the pumps both stop which is normal.

The problem is, when I push button to the 4th time, 5th time, 6th time etc., the water pumps will not come back on even when it comes back around to position to on the 2nd button. (button 2 set an LED tosix different Frightening positions which is working fine.)

I’m wanting to add different lengths of oscillation time on the water pumps on each one of those presses. This is my main goal. Everything else is working fine.

[code]//added in 2 buttons to control the length of wave time on this waveMaker Auto Top Off code.

//buttons
const int numOfInputs = 2;
const int inputPins[numOfInputs] = {2, 3};
const int outputPin = 10;
int LEDState = 0;
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW};
int inputCounters[numOfInputs];
long lastDebounceTime[numOfInputs] = {0, 0};
long debounceDelay = 50;

int pumpEvent1;//////////////////////////////////////////////////////////////////////
int pumpEvent2;/////////////////////////////////////////////////////////////////////
int pumpEvent3;//////////////////////////////////////////////////////////////////////
int pumpEvent4;/////////////////////////////////////////////////////////////////////

const unsigned long waveLength1 = 1000;//////////////////////////////////////////////
const unsigned long waveLength2 = 4000;/////////////////////////////////////////////////

//waveMaker code
#include "Timer.h"

Timer t;

int pump1 = 4;
int switchTime = 1000;
int interval = 0;
int waveState;
int waveMaker;
int pump2 = 5;

//ATO stuff next
byte lowWaterPin = 6;    // Goes LOW when water below switch
byte highWaterPin = 7;   // Goes LOW when water below switch
int pumpPin = 12;        // Relay to control the water solenoid

unsigned long maxRunTime = 60 * 1000L;   // pump on for max of one minute
unsigned long switchDebounceTime = 2 * 1000L;  // Switch must be activated for at least 3 seconds.
unsigned long minOffTime = 2 * 1000L;  // pump must be off for at least one hour

unsigned long lastPumpTime = 0;
unsigned long lastLowWaterDetectTime = 0;

boolean lastLowWaterState = HIGH;
boolean pumpRunning = false;


//Back to WaveMaker
void setup()

{
  pumpEvent1 = t.oscillate(pump1, waveLength1, HIGH);///////////////////////////////////////////////////
  pumpEvent2 = t.oscillate(pump2, waveLength1, LOW);//////////////////////////////////////////////////////
  pumpEvent3 = t.oscillate(pump1, waveLength2, HIGH);///////////////////////////////////////////////////
  pumpEvent4 = t.oscillate(pump2, waveLength2, LOW);//////////////////////////////////////////////////////

  pinMode(pump1, OUTPUT);
  pinMode(pump2, OUTPUT);
  unsigned long currentMillis = 1000;
  
  //buttons
  for (int i = 0; i < numOfInputs; i++) {
    pinMode(inputPins[i], INPUT);
    digitalWrite(inputPins[i], HIGH); // pull-up 20k

  }

  pinMode(outputPin, OUTPUT);
  Serial.begin(9600);

  //ATO stuff from here
  pinMode(lowWaterPin, INPUT_PULLUP);
  pinMode(highWaterPin, INPUT_PULLUP);
  pinMode(pumpPin, OUTPUT);

}


//waveMaker from here
void loop()

{
 //buttons
  setInputFlags();
  resolveInputFlags();
  resolveOutputs();

  t.update();

  //From here on is the ATO code
  boolean lowWaterState = digitalRead(lowWaterPin);
  boolean highWaterState = digitalRead(highWaterPin);

  unsigned long currentMillis = millis();


  if (lowWaterState != lastLowWaterState) {
    lastLowWaterDetectTime = currentMillis;

  }

  if (pumpRunning) {  // if the pump is on then let's see if we should turn it off yet

    if ((highWaterState == LOW) || (currentMillis - lastPumpTime >= maxRunTime)) {
      digitalWrite(pumpPin, LOW);
      pumpRunning = false;
      lastPumpTime = currentMillis;

    }

  }

  else {   // pump is not running, see if we need to turn it on

    if ((lowWaterState == HIGH)  &&  (currentMillis - lastLowWaterDetectTime >= switchDebounceTime) && (currentMillis - lastPumpTime > minOffTime)) { // switch is low and has been for at least 3 seconds
      digitalWrite(pumpPin, HIGH);
      pumpRunning = true;
      lastPumpTime = currentMillis;

    }

  }
  lastLowWaterState = lowWaterState;
}


void takeReading()

{

  Serial.println(analogRead(0));

}


void updateWave() {
  if (millis() - switchTime >= interval)  // #define interval as appropriate
  {
    switchTime += interval ;  // bump switchTime on for next time
  }


  if (waveState)
    digitalWrite(waveMaker, HIGH);
  else
    digitalWrite(waveMaker, LOW);

  Serial.print(waveState);
  Serial.print(", ");
  Serial.print(switchTime);
  Serial.print(", ");
  Serial.println(millis());


  boolean islooped;
  if (millis() >= 10000 && !islooped)
  {
    islooped = true;
  }
  if (islooped && millis() <= 10000)
  {
    islooped = false;
    switchTime = millis();
  }
}


//buttons from here on out
void setInputFlags() {

  for (int i = 0; i < numOfInputs; i++) {
    int reading = digitalRead(inputPins[i]);
    if (reading != lastInputState[i]) {
      lastDebounceTime[i] = millis();

    }

    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading != inputState[i]) {
        inputState[i] = reading;

        if (inputState[i] == HIGH) {

          inputFlags[i] = HIGH;

        }

      }

    }

    lastInputState[i] = reading;

  }

}



void resolveInputFlags() {

  for (int i = 0; i < numOfInputs; i++) {

    if (inputFlags[i] == HIGH) {

      // Input Toggle Logic

      inputCounters[i]++;

      updateLEDState(i);

      printString(i);

      inputFlags[i] = LOW;

    }

  }

}



void printString(int output) {

  Serial.print("Input ");
  Serial.print(output);
  Serial.print(" was pressed ");
  Serial.print(inputCounters[output]);
  Serial.println(" times.");

}


void updateLEDState(int input) {

  // input 0 = State 0 and 1

  if (input == 0) {                     //brings the dimming led back to 0 if the on off button is pushed.  jb

    if (LEDState == 0) {               //the on off button is input 0. jb

      LEDState = 1;                    //the 6 posistion button is input 1. settings 2-6 jb

    } else {

      LEDState = 0;

    }

    // input 1 = State 2 to 6

  } else if (input == 1) { // 2,3,4,5,6,2,3,4,5,6,2,

    if (LEDState == 0 || LEDState == 1 || LEDState > 5) {

      LEDState = 2;                        //these last 2 lines bring the led 2 back to brightness level 2. buttin input 1 is levels 2-6 jb

    } else {

      LEDState++;

    }

  }

}



void resolveOutputs() {

  switch (LEDState) {
  
    case 0:

      digitalWrite(outputPin, LOW);

      digitalWrite(pump1, LOW);
      digitalWrite(pump2, LOW);

      break;

    case 1:

      digitalWrite(outputPin, HIGH);

      digitalWrite(pump1, HIGH);
      digitalWrite(pump2, HIGH);

      break;

    case 2:
  

      analogWrite(outputPin, 30);

        pumpEvent1;/////////////////////////////////////////////////////////////////////////

        pumpEvent2;//////////////////////////////////////////////////////////////////////////    

      break;
      
    case 3:

      analogWrite(outputPin, 70);
            
 t.stop(pumpEvent1);
       t.stop(pumpEvent2);
       
        pumpEvent3;/////////////////////////////////////////////////////////////////////////

        pumpEvent4;//////////////////////////////////////////////////////////////////////////    
 
         break;
      
  
    case 4:
     t.stop(pumpEvent3);
       t.stop(pumpEvent4);
   

      digitalWrite(pump1, LOW);
      digitalWrite(pump2, LOW);

      analogWrite(outputPin, 100);
          
      break;

    case 5:      

   analogWrite(outputPin, 155);


      break;

    case 6:

      analogWrite(outputPin, 255);

      break;

    default:

      break;

  }

}

[/code]

Tom, you asked me what t.oscillate is. Here is a really good link.

https://github.com/jtwalters/arduino-alarm/blob/master/libraries/Timer/examples/blink2/blink2.ino

I’m using an Uno and Nano

Basically what I’m trying to accomplish is for my pumps to return back to “pumpEvent1” and “pumpEvent2” whenever I press the button the 6th time to go back into “case 2:” The “LEDState “ returns back to dim on “case 2:” as it is supposed to but the pumps will not return back to pumpEvent1 and 2.

Thank you for any help you can give me.

The portion of the code I am speaking of is the very last section. Thank you.

I’m trying a completely different approach. Eventually.h