Interractive model using arduino, solenoid valves and relays

​​Code

#include <SharpIR.h>

#define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)
int sol1 = 7;
int sol2 = 8;
int sol3 = 9;

float coeficient = 0.0048828125;
void setup() {
pinMode(sol1, OUTPUT); // sets the digital pin as output
pinMode(sol2, OUTPUT); // sets the digital pin as output
pinMode(sol3, OUTPUT); // sets the digital pin as output

Serial.begin(9600); // start the serial port
}

void loop()
{
// 5v
float volts = analogRead(sensor) * coeficient; // value from sensor * (5/1024)
int distance = 13 * pow(volts, -1); // worked out from datasheet graph
delay(100); // slow down serial port
if (distance == 0)
{
distance = 50;
}
Serial.println(distance); // print the distance

if (distance < 50)
{
digitalWrite(sol1, HIGH); // sets the LED on
Serial.println("sol1");
}
else
{
digitalWrite(sol1, LOW); // sets the LED off
}
if (distance < 35)
{
digitalWrite(sol2, HIGH); // sets the LED on
Serial.println("sol2");
}
else
{
digitalWrite(sol2, LOW); // sets the LED off
}
if (distance < 20)
{
digitalWrite(sol3, HIGH); // sets the LED on
Serial.println("sol3");
}
else
{
digitalWrite(sol3, LOW); // sets the LED off
}
}

 

Leave a Reply

Your email address will not be published.