Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Shu ZhangXi Liu
Created April 26, 2018

SunFlowerPot

SunFlowerPot allows plants to freely chase the sun and water according to their own needs, detecting environmental conditions.

89
SunFlowerPot

Things used in this project

Story

Read more

Schematics

wire_bb_Gg1vktDDO7.jpg

Code

sunflowerpot_code

Arduino
#include <IRremote.h>
#include "DHT.h"
#include <Wire.h>
#include <BH1750.h>

DHT dht;
BH1750 lightSensor;

int on = 0;
unsigned long last = millis();

const int SensorRight_2 = 5;
const int SensorLeft_2 = 6;
int Left_motor_pwm = 9;
int Right_motor_pwm = 10;

int SR_2;
int SL_2;

int s0 = 13;
int s1 = 12;
int s2 = 11;


void setup()
{
  pinMode(Left_motor_pwm, OUTPUT);
  pinMode(Right_motor_pwm, OUTPUT);
  pinMode(SensorRight_2, INPUT);
  pinMode(SensorLeft_2, INPUT);
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);

  Serial.begin(9600);
  Wire.begin();
  lightMeter.begin();
  dht.setup(2);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  LightSensor.begin();
  LightSensor.SetAddress(Device_Address_L);
  LightSensor.SetMode(Continuous_H_resolution_Mode);
  Serial.println("Running...");
}

void run()
{
  digitalWrite(Right_motor_pwm, HIGH);
  analogWrite(Right_motor_pwm, 100)
  digitalWrite(Left_motor_pwm, HIGH);
  analogWrite(Left_motor_pwm, 100);
}

void brake()
{
  analogWrite(Right_motor_pwm, 0);
  analogWrite(Left_motor_pwm, 0);
}

void spin_left()
{
  digitalWrite(Right_motor_pwm, HIGH);
  analogWrite(Right_motor_pwm, 100);
  digitalWrite(Left_motor_pwm, LOW);
  analogWrite(Left_motor_pwm, 0);
}

void spin_right()
{
  digitalWrite(Right_motor_pwm, LOW);
  analogWrite(Right_motor_pwm, 0);
  digitalWrite(Left_motor_pwm, HIGH);
  analogWrite(Left_motor_pwm, 100);
}

void back()
{
  digitalWrite(Right_motor_pwm, HIGH);
  analogWrite(Right_motor_pwm, 100);
  digitalWrite(Left_motor_pwm, HIGH);
  analogWrite(Left_motor_pwm, 100);
}

void Robot_Avoidance()
{
  SR_2 = digitalRead(SensorRight_2);
  SL_2 = digitalRead(SensorLeft_2);
  if (SL_2 == HIGH && SR_2 == HIGH)
  {
    run();
  }
  else if (SL_2 == HIGH & SR_2 == LOW)
    spin_left();
  else if (SR_2 == HIGH & SL_2 == LOW)
    spin_right();
  else
  {
    brake();
    delay(300);
    back();
    delay(400);
    spin_left();
    delay(500);
  }
}

int readMux(int channel) {
  int controlPin[] = {s0, s1, s2};
  int muxChannel[5][3] = {
    {0, 0, 0}, //channel 0
    {1, 0, 0}, //channel 1
    {0, 1, 0}, //channel 2
    {1, 1, 0}, //channel 3
    {0, 0, 1}, //channel 4
  };

  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }
}

void loop()
{
  int soilMoister, air, temperature, humidity;
  uint16_t lux[5];
  Robot_Avoidance();
  soilMoister = digitalRead(3);
  air = digitalRead(1);
  uint16_t lux = lightMeter.readLightLevel();
  Serial.print("Soil Moister:");
  Serial.println(soilMoister);
  Serial.print("Humidity:");
  Serial.println(dht.getHumidity());
  Serial.print("Temperature:");
  Serial.println(dht.getTemperature());
  Serial.print("Air Quality:");
  Serial.println(air);
  for (int i = 0; i < 5; i ++) {
    readMux(i);
    LightSensor.begin();
    LightSensor.SetAddress(Device_Address_L);
    LightSensor.SetMode(Continuous_H_resolution_Mode);
    lux[i] = LightSensor.GetLightIntensity();
    Serial.print("L");
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.println(lux[i]);
  }
  delay(1000);
}

Credits

Shu Zhang

Shu Zhang

1 project • 2 followers
Xi Liu

Xi Liu

0 projects • 0 followers

Comments