Blog

WATER AND SOAP DISPENSER

The code can be downloaded at : https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Tap%20Water%20and%20Soap%20dispenser.zip


int led = 5;
int OUTPUT_PIN = 7;
int INPUT_PIN = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT); 
  pinMode(OUTPUT_PIN, OUTPUT);
  pinMode(INPUT_PIN, INPUT);  

}
int on = 0;
unsigned long last = millis();
void loop()
{
  
  
 int sensorValue0 = analogRead(A0); 

   if (sensorValue0 < 300) 
{
  
if (millis() - last > 250) {
digitalWrite(led, HIGH);  
  delay(500);               
  digitalWrite(led, LOW);    
  delay(1000); 
    }
    //last = millis();
}

  if (digitalRead(INPUT_PIN) == LOW) {
        //==============================
    if (millis() - last > 250) {
      on = !on;
      //analogWrite(OUTPUT_PIN, on ? 255 : 0);
      digitalWrite(OUTPUT_PIN, on ? HIGH : LOW);
    }
    last = millis();      
    //=================================================== 
}

  else
  {
  digitalWrite(led, LOW); 
  }
}
Advertisement

TAP WATER SWITCH BY HC-SR04

The code can be downloaded at : https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Switch%20Tap%20Water.zip

#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  2
#define  ECHO_PIN     4 // don't use pin 3
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;
long duration, distance, UltraSensor;
int OUTPUT_PIN = 5;
void setup() 
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  pinMode(OUTPUT_PIN, OUTPUT); 
}
int on = 0;
unsigned long last = millis();

void loop() 
{
  delay(10);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  Serial.print("Ping: ");
  Serial.print(DistanceIn); // Convert ping time to distance and print result 
                            // (0 = outside set distance range, no ping echo)
  Serial.print(" in     ");
  
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.print(DistanceCm); 
  Serial.println(" cm");  
  
  
  if (DistanceCm < 20) {//this value can be adjusted for get desired sensitivity
  
        //==============================
    if (millis() - last > 250) {
      on = !on;
      analogWrite(OUTPUT_PIN, on ? 255 : 0);
      //digitalWrite(OUTPUT_PIN, on ? HIGH : LOW); if do not want adjustable speed
    }
    last = millis();      
    //=================================================== 
   
    
    //digitalWrite(OUTPUT_PIN,HIGH); 
    //Serial.println(val); 
   
} 
}

TAP WATER VALVE SWITCH

The code can be downloaded at : https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/TapWaterSwitch.zip

int sensorpin = 4;
int val = 0;
int OUTPUT_PIN = 4;
void setup()
{
Serial.begin(9600);
pinMode(OUTPUT_PIN, OUTPUT);
}
int on = 0;
unsigned long last = millis();
void loop()
{
val = analogRead(sensorpin);
if (val > 300) {//this value can be adjusted for get desired sensitivity

digitalWrite(OUTPUT_PIN,HIGH); 
//Serial.println(val); 

}

else{
//Serial.println(val);
//delay(100);
digitalWrite(OUTPUT_PIN,LOW);
}
}

MEASURING CAPASITOR

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Measuring%20Capasitor.zip

const int OUT_PIN = A2;
const int IN_PIN = A0;
const float IN_STRAY_CAP_TO_GND = 24.48;
const float IN_CAP_TO_GND = IN_STRAY_CAP_TO_GND;
const float R_PULLUP = 34.8;
const int MAX_ADC_VALUE = 1023;

void setup()
{
pinMode(OUT_PIN, OUTPUT);
pinMode(IN_PIN, OUTPUT);
Serial.begin(9600);
}

void loop()
{
pinMode(IN_PIN, INPUT);
digitalWrite(OUT_PIN, HIGH);
int val = analogRead(IN_PIN);
digitalWrite(OUT_PIN, LOW);

if (val < 1000)
{
  pinMode(IN_PIN, OUTPUT);

  float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);

  Serial.print(F("Capacitance Value = "));
  Serial.print(capacitance, 3);
  Serial.print(F(" pF ("));
  Serial.print(val);
  Serial.println(F(") "));
}
else
{
  pinMode(IN_PIN, OUTPUT);
  delay(1);
  pinMode(OUT_PIN, INPUT_PULLUP);
  unsigned long u1 = micros();
  unsigned long t;
  int digVal;

  do
  {
    digVal = digitalRead(OUT_PIN);
    unsigned long u2 = micros();
    t = u2 > u1 ? u2 - u1 : u1 - u2;
  } while ((digVal < 1) && (t < 400000L));

  pinMode(OUT_PIN, INPUT);  
  val = analogRead(OUT_PIN);
  digitalWrite(IN_PIN, HIGH);
  int dischargeTime = (int)(t / 1000L) * 5;
  delay(dischargeTime);   
  pinMode(OUT_PIN, OUTPUT);  
  digitalWrite(OUT_PIN, LOW);
  digitalWrite(IN_PIN, LOW);

  float capacitance = -(float)t / R_PULLUP
                          / log(1.0 - (float)val / (float)MAX_ADC_VALUE);

  Serial.print(F("Capacitance Value = "));
  if (capacitance > 1000.0)
  {
    Serial.print(capacitance / 1000.0, 2);
    Serial.print(F(" uF"));
  }
  else
  {
    Serial.print(capacitance, 2);
    Serial.print(F(" nF"));
  }

  Serial.print(F(" ("));
  Serial.print(digVal == 1 ? F("Normal") : F("HighVal"));
  Serial.print(F(", t= "));
  Serial.print(t);
  Serial.print(F(" us, ADC= "));
  Serial.print(val);
  Serial.println(F(")"));
}
while (millis() % 1000 != 0)
  ;    

}

HALL SENSOR SWITCH

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/HallSensorSwitch.zip

// constants won’t change. They’re used here to
// set pin numbers:
const int OutputPin = 2; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin

// variables will change:
int HallState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(OutputPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
HallState = digitalRead(OutputPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (HallState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}