Pages: [1]   Go Down
Author Topic: Modifications in function  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello there !
In the following sketch, I was using  three reed switches and only one magnetic float(when the magnetic float was traveling upwards and downwards, it was making and breaking the reed switches) , now I want to use three separate  magnetic float switches (each switch will have its own reed switch and magnetic float)

What modifications, I need to do in the functions in this sketch?

Thanks

Code:
#include <EEPROM.h>

// INPUT PINS
#define Buttonpin1  2  //Tank Level1
#define Buttonpin2  3  //Tank Level2
#define Buttonpin3  4  //Tank Level3
#define Auto_Man_Buttonpin   5 //Auto/manual button

// OUTPUT PINS
#define LED1  8 //Tank1 Level1 Display
#define LED2  9 //Tank1 Level2 Display
#define LED3  10 //Tank2 Level1 Display
#define Auto_Man_Led  6  //Auto/manual operation
#define Pumppin 7 // Pin for water pump activation

// EEPROM MEMORY POSITIONS
#define memposL1  0  // set constant eeprom position 0 to save Level

// variables
int state=0;
int level = 0;
int lastlevel = 0;
void setup (){

// INPUTS
pinMode(Auto_Man_Buttonpin,INPUT);  // default mode is INPUT
pinMode(Buttonpin1,INPUT);               // default mode is INPUT
pinMode(Buttonpin2,INPUT);               // default mode is INPUT
pinMode(Buttonpin3,INPUT);               // default mode is INPUT
digitalWrite(Auto_Man_Button, HIGH);  // Turn on the internal pull-up resistor, default state is HIGH
digitalWrite(Buttonpin1, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH
digitalWrite(Buttonpin2, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH
digitalWrite(Buttonpin3, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH

// OUTPUTS
pinMode(Pumppin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(Auto_Man_Led, OUTPUT);
pinMode(LED3, OUTPUT);

// Restore tank water Level from eeprom
level = EEPROM.read(memposL1);  // Read from eeprom memPos1 the value of Level1
lastlevel = level;                          // also update the according lastLevel variable
if ((level == 0)||(level>3))            // if eeprom has illegal value
{        
level=1;              // set level to 1 (minimum)
updateprom(); // call updateprom function to set eeprom memory value to minimum too.
}
setleds();       // restore the reed states from EEPROM stored value.
delay(1000);
startstoppump();
}

// FUNCTION THAT SETS THE LEDS ACCORDING TO LEVEL VARIABLE WHENEVER CALLED
void setleds()
{

 digitalWrite(LED1, LOW );
 digitalWrite(LED2, LOW );
 digitalWrite(LED3, LOW );
 if (level > 0)   digitalWrite(LED1, HIGH );
 if (level > 1)   digitalWrite(LED2, HIGH );
 if (level > 2)   digitalWrite(LED3, HIGH );
}

// FUNCTION THAT CONTROLS THE PUMP OPERATION ACCORDING TO WATER LEVEL AND AUTO_MAN SETTING


  void  startstoppump(){
  if (level < 3)   digitalWrite(Pumppin, HIGH );
  if ((level == 3) & (state == 0))
{
  digitalWrite(Pumppin, LOW );
}
else
{
digitalWrite(Pumppin, HIGH );
}
 }

// FUNCTION THAT UPDATES EEPROM AT EACH LEVEL CHANGE
void updateprom()
{                           // UPDATE LEVEL 1
 
lastlevel = level;       // update lastLevel variable
EEPROM.write(memposL1, level);   // and store new Level value to eeprom
}

// MAIN LOOP
void loop ()
{

// TOGGLE AUTO/MANUAL MODE
  if ( (digitalRead(Auto_Man_Buttonpin) == LOW) & (state == 0) )
{                                                            // Button  pushed
state=1;  
digitalWrite(Auto_Man_Led, state );            // Turn on the LED
startstoppump();
delay(400);
}
  if ( (digitalRead(Auto_Man_Buttonpin) == LOW) & (state == 1)  )
{                                                           // Button  pushed
  state=0;  
  digitalWrite(Auto_Man_Led, state );        // Turn off the LED
   startstoppump();
  delay(400);
}

//END OF TOGGLE AUTO/MANUAL MODE
 
// CHECK WATER LEVEL
if ( digitalRead(Buttonpin1) == LOW  )
{                              // Button  pushed
level = 1;
 }
 if ( digitalRead(Buttonpin2) == LOW  )
{                               // Button  pushed
 level = 2;
}
 if ( digitalRead(Buttonpin3) == LOW  )
{                              // Button  pushed
level = 3;
}
//END OF CHECK WATER LEVEL

 //  IF WATER LEVEL CHANGED AND CALL THE NECESSARY FUNCTIONS.
 if (level != lastlevel)
{
 setleds();
 startstoppump();
 updateprom();
 }  
  } // END OF MAIN LOOP


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What modifications, I need to do in the functions in this sketch?
First
thing
you
need
to
do
is
use
Tools
+
Auto
Format
to
make
the
code
readable.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25760
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
define Buttonpin1  2  //Tank Level1
#define Buttonpin2  3  //Tank Level2
#define Buttonpin3  4  //Tank Level3
#define Auto_Man_Buttonpin   5 //Auto/manual button

// OUTPUT PINS
#define LED1  8 //Tank1 Level1 Display
#define LED2  9 //Tank1 Level2 Display
#define LED3  10 //Tank2 Level1 Display
#define Auto_Man_Led  6  //Auto/manual operation
#define Pumppin 7 // Pin for water pump activation
If an input is a tank-level input, why not call it that?
If an LED indicates a tank-level, why not call it that?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the tips
I am Sorry for the mess up, here is new format compiled code.

Code:
#include <EEPROM.h>
// CONSTANTS
// INPUT PINS
#define Level1  3 
#define Level2  4 
#define Level3  5 

// OUTPUT PINS
#define LEDLevel1  8
#define LEDLevel2  9
#define LEDLevel3  11
#define Pumppin  13
// EEPROM MEMORY POSITIONS
#define memposL1  0  // set constant eeprom position 0 to save Level1
// variables
int state= 0;
int level = 0;
int lastlevel = 0;
void setup ()
{
  // INPUTS

  pinMode(Level1,INPUT);   // default mode is INPUT
  pinMode(Level2,INPUT);   // default mode is INPUT
  pinMode(Level3,INPUT);   // default mode is INPUT

  digitalWrite(Level1, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH
  digitalWrite(Level2, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH
  digitalWrite(Level3, HIGH);     // Turn on the internal pull-up resistor, default state is HIGH
  // OUTPUTS
  pinMode(Pumppin, OUTPUT);
  pinMode(LEDLevel1,    OUTPUT);
  pinMode(LEDLevel2,    OUTPUT);
  pinMode(LEDLevel3,    OUTPUT);
  // Restore tank water Level from eeprom
  level = EEPROM.read(memposL1);   // Read from eeprom memPos1 the value of Level1
  lastlevel = level;    // also update the according lastLevel variable
  if ((level == 0)||(level>3))
  {                               // if eeprom has illegal value
    level=1; // set level to 1 (minimum)
    updateprom();        // call updateprom function to set eeprom memory value to minimum too.
  }
  setleds();                // restore the reed states from EEPROM stored value.
  delay(1000);
  startstoppump();
}
// FUNCTION THAT SETS THE LEDS ACCORDING TO LEVEL VARIABLE WHENEVER CALLED
void setleds()
{

  digitalWrite(LEDLevel1, LOW );
  digitalWrite(LEDLevel2, LOW );
  digitalWrite(LEDLevel3, LOW );
  if (level > 0)   digitalWrite(LEDLevel1, HIGH );
  if (level > 1)   digitalWrite(LEDLevel2, HIGH );
  if (level > 2)   digitalWrite(LEDLevel3, HIGH );
}
//FUNCTION THAT CONTROL THE START AND TOP OF THE PUMP
void  startstoppump()
{
  if (level == 1)   
  {
    digitalWrite(Pumppin, HIGH );
  }
  if ((level == 3) & (state == 0))
  {
    digitalWrite(Pumppin, LOW );
  }
}
// FUNCTION THAT UPDATES EEPROM AT EACH LEVEL CHANGE
void updateprom()
{
  lastlevel = level;                           // update lastLevel variable
  EEPROM.write(memposL1, level);   // and store new Level value to eeprom
}
// MAIN LOOP
void loop ()
{
  // CHECK WATER LEVEL
  if ( digitalRead(Level1) == LOW  )
  {
    level = 1;
  }
  if ( digitalRead(Level2) == LOW  )
  {
    level = 2;
  }
  if ( digitalRead(Level3) == LOW  )
  {
    level = 3;
  }
  //END OF CHECK WATER LEVEL
  //  IF WATER LEVEL CHANGED AND CALL THE NECESSARY FUNCTIONS.
  if (level != lastlevel)
  {
    setleds();
    startstoppump();
    updateprom();
  }   
} // END OF MAIN LOOP




Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if ((level == 3) & (state == 0))
Should that be && in the middle? I can't see how a bitwise and is useful.

In your code, I can't see that changing the mechanism for triggering the switch will make any difference. The switches are still HIGH or LOW, regardless of what caused them to be HIGH or LOW.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I can't see how a bitwise and is useful.
in this particular case it should work as well as logical AND, but I agree it should &&
Logged

Per Arduino ad Astra

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More to the point, why are you testing state at all - you never change it.

What's the purpose of the change to three float switches?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Mr. PaulS,
You are right, I will change it to &&.

Code:
What's the purpose of the change to three float switches?

 
Thanks Mr. Wildbill
This one float system sometime mal-functions. Another problem is that if the water falls below the
Minimum i.e. level1 the indicator ledLevel1 remains ON. What is the solution to this problem ?





Logged

Pages: [1]   Go Up
Jump to: