Final Project Description

 For my final project, I designed a cardboard car. This car uses a soil moisture sensor and water sensor. When the soil moisture sensor is activated, an alarm plays from a laptop which can be turned up or down with a connected potentiometer. An LED lights up on the roof of the car and this turns off alongside the audio when no moisture is sensed.

The water sensor is attached to the roof of the car. When this is activated, a gif of rain drops begins to play on the laptop, and a servo acts as a windshield wiper and moves repeatedly at 180 degree angles. Within the casing, an LCD light shows "Y" for water sensed, or "N" when no water is sensed. I initially wanted to add a heat pad and fan that activated when moisture was sensed, but I was not able to accomplish this.

                                                               




Here are videos of the device in action:



Here is my code:

// Importing libraries
import processing.serial.*;
import cc.arduino.*;
import ddf.minim.*;
import gifAnimation.*;

// Defining variables
Arduino arduino;
Minim minim;
AudioPlayer alarmSound;
Gif myAnimation;

// Defining digital output pins
int ledPin = 11;
int servoPin = 12;

// Definining analog input pins
int soilPin = 0;
int potentiometerPin = 3;
int waterSensorPin = 5;

// Defining thresholds
int waterThreshold = 200;
int soilThreshold = 100;

// Empty int variables
int waterSensorValue;
int soilSensorValue; 
int potentiometerValue;

// Defining timer variables
int startTime;
int endTime;
int interval;
boolean rotatedServo = false;

// Run first time
void setup() {
    // Creates canvas
    size(498, 264);
    background(0);

    // Set servo timer to 0
    startTime = millis();
    interval = 500;

    // Initialising gif
    myAnimation = new Gif(this, "rain.gif");
    myAnimation.play();

    // Initialising audio
    minim = new Minim(this);
    alarmSound = minim.loadFile("alarm.wav", 2048);
    alarmSound.loop();
    alarmSound.mute();

    // Print list of ports
    println(Arduino.list());

    // Modify this line, by changing the "0" to the index of the serial
    // port corresponding to your Arduino board (as it appears in the list
    // printed by the line above).
    arduino = new Arduino(this, Arduino.list()[5], 57600);

    // Setting pins 0-13 to output
    for (int i = 0; i <= 13; i++) {
        arduino.pinMode(i, Arduino.OUTPUT);
        arduino.digitalWrite(ledPin, Arduino.LOW);
        arduino.pinMode(servoPin, Arduino.SERVO);
    }
}

// Calling all functions that get sent to the arduino
void draw() {
    endTime = millis();
    waterSensorStart();
    potentiometerControl();
    soilMoistureDetectorStart();
}

// Creates function for letter Y on 7-segment display
void letterOne() {
    arduino.digitalWrite(4, Arduino.LOW);
    arduino.digitalWrite(8, Arduino.LOW);
    arduino.digitalWrite(2, Arduino.HIGH);
    arduino.digitalWrite(3, Arduino.HIGH);
    arduino.digitalWrite(5, Arduino.HIGH);
    arduino.digitalWrite(6, Arduino.HIGH);
    arduino.digitalWrite(7, Arduino.HIGH);
}

// Creates function for letter N on 7-segment display
void letterTwo() {
    arduino.digitalWrite(2, Arduino.LOW);
    arduino.digitalWrite(5, Arduino.LOW);
    arduino.digitalWrite(3, Arduino.HIGH);
    arduino.digitalWrite(4, Arduino.HIGH);
    arduino.digitalWrite(6, Arduino.HIGH);
    arduino.digitalWrite(7, Arduino.HIGH);
    arduino.digitalWrite(8, Arduino.HIGH);
}

// Creates function to move servo like a windshield wiper with no delays
void windshieldWipe() {
    if(endTime - startTime >= interval){
        if(!rotatedServo) {
            arduino.servoWrite(servoPin, 180);
            startTime = millis();
            rotatedServo = true;
        }
        else {
            arduino.servoWrite(servoPin, 0);
            startTime = millis();
            rotatedServo = false;
        }
    }
}

// Creates function to detect water on the water sensor, which calls letterOne()/letterTwo() and windshieldWipe(), and plays a gif of rain
void waterSensorStart() {
    waterSensorValue = arduino.analogRead(waterSensorPin);
    if (waterSensorValue > waterThreshold) {
        println("Water is detected!");
        windshieldWipe();
        letterOne();
        image(myAnimation, 0, 0);
    } 
    else {
        arduino.servoWrite(servoPin, 0);
        letterTwo();
        println("Water is NOT detected!");
        background(0);
    }
}

// Creates function to detect potentiometer value, and adjust gain of alarm accordingly
void potentiometerControl() {
    int potentiometerValue = arduino.analogRead(potentiometerPin);
    alarmSound.setGain((potentiometerValue/10)-50);
    println("Potentiometer value:", potentiometerValue);
}

// Creates function to detect moisture in soil, which lights up an LED and plays an alarm
void soilMoistureDetectorStart() {
    soilSensorValue = arduino.analogRead(soilPin);
    if (soilSensorValue > soilThreshold) {
        println("Soil moisture is DETECTED. The moisture level is: ", soilSensorValue);
        arduino.digitalWrite(ledPin, Arduino.HIGH);
        alarmSound.unmute();
    }
    else {
        println("Soil moisture is low. The moisture level is: ", soilSensorValue);
        arduino.digitalWrite(ledPin, Arduino.LOW);
        alarmSound.mute();
    }
}

Initial idea sketches:

Comments