Introversive Mushroom

I got the code works well on bread board. When you are close to the "eyes", the motor runs, the distance is less than the safe distance. However, it will stop running if you water it to the comfortable humidity, and it will start running if you over water it. 

From last week, I was working on servo motor, and I have to use two motors for my mushroom, which is too heavy. Then I found the DC motor with gears, so I can only use one motor. Also, compared with servo motor, this motor only need 3v battery. The motor was shipped from Japan and need to be assembled by hand. (Mini Motor Low-Speed Gearbox (4-Speed))

 I assembled the motor. 

I assembled the motor. 

I wrote the whole code and cleaned it through adding modular function. However, it didn't work after that, and it even couldn't print Serial Read. After changing some sequences and position of code, it worded like the video I show. 

Then I tried moving it to PCB. I designed the new PCB and double-sided printed it. 

 The PCB was printed really nice. 

The PCB was printed really nice. 

#include "DHT.h"
#include <Servo.h>
#define DHTTYPE DHT11   // DHT 11 
const int DHTPIN = 2;     // what pin we're connected to

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
float h = 0;
const int comfortHumidity = 49;
const int overHumidity = 65; // the mushroom feels comfortable when humidity is between 45% to 58%

const int trigPin = 13; // the ultrasonic sensor pins
const int echoPin = 12;
const int led = 11;
float duration;
float d;
const int safeDistance = 15; //the safe distance of activating the mushroom


Servo myservo;
int pos = 0; // the original position of servo

void setup() {
  Serial.begin(9600);
  Serial.println("test");
  //  dht.begin();
  //myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(9, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  h = dht.readHumidity();
}

void loop() {

  h = dht.readHumidity();
  readHumidity();
  Serial.print("d: ");
  Serial.println(d);
  readDistance();

  if (d > safeDistance) {  // This is where the LED and motor Off happens
    digitalWrite(led, LOW);
    calmDown();
    if ((h >= comfortHumidity) && (h < overHumidity))
    {
      Serial.println("strolling");
      strolling();
    }
  } // distance is greater than 15, the mushroom doesn't move

  else if (d <= safeDistance) { // This is when the
    digitalWrite(led, HIGH);
    if ((h < comfortHumidity) || (h >= overHumidity))
    {
      runAway();
    }
    else if ((h >= comfortHumidity) && (h < overHumidity))
    {
      calmDown();
    }
  }// distance is greater than 15
}

void readDistance() //read the value of ultrasonic sensor
{
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  d = (duration / 58);
  Serial.print(d);
  Serial.println(" cm");
  delay(100);
}

void readHumidity() //read and print the humidity
{
  if (isnan(h) ) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
}

void runAway() //the mushroom moves back and forth
{
  analogWrite(9, 255);

  //  for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
  //  { // in steps of 1 degree
  //    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //    Serial.println("Moving Forward");
  //    delay(15);                       // waits 15ms for the servo to reach the position
  //  }
  //  for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
  //  {
  //    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //    Serial.println("Moving Backward");
  //    delay(15);                       // waits 15ms for the servo to reach the position
  //  }
}

void strolling() //the mushroom moves slowlier when it feels safe
{
  analogWrite(9, 0);
  //  for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
  //  { // in steps of 1 degree
  //    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //    Serial.println("Moving Forward");
  //    delay(100);
  //  }
  //  for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
  //  {
  //    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  //    Serial.println("Moving Backward");
  //    delay(100);
  //  }
}


void calmDown() //the motor stops
{
  Serial.println("Mushroom calms down");
  analogWrite(9, 0);
}

Final Project: Concept-Scared Mushroom

My project is to add motion and characteristics to a mushroom. 

The mushroom is very timid. When people are close to it, it will run away.

However, try being patient to it. It is just introversive! If you spray water on it, it knows that you just want to water it and mean no harm to it, then it will stop running. 

The inside structure will be a challenge for me. I need to figure out the effective place that the sensors live. 

I need to figure out how the batteries connect to my model. Will it be too heavy?

I am worried if the humidity can work well when the motor already started running and how long it can sense the humidity change. 

Week 11 First PCB - Secret Flowerpot 3.0

Following the mid-term project, I designed the H-bridge PCB this week. It is challenging for me to design such a complex CB

but I want to improve the last project through minimalizing the circuit board. The size of double printing PCB is limited in Hybrid Lab, so I tried to design it as one-side board. However, I found the wires linked to push buttons are all locked on the top of the board. Also, I had one wire which was hard to separate from other wires, so I left it on the top layer. I will solder these wires later. 

Click on the following picture to see the process and printed board.