top of page

Challenges we encountered:

how to detect whether the mobile phone has been placed in the certain area.

1. reed switch  (Electromagnetic induction)

Cell phone magnetism is very small, the effect is not obvious 

2. IC: lm386 audio amplifier 

​      we followed this tutorial: https://www.instructables.com/How-to-Make-Mobile-Phone-Detector/

What is the principle? Is it related to the copper wire as the result is not good...

 

When the copper wire contacts with any object that can conduct electricity, the light will be on. When we put the phone near the wire, the brightness is hardly visible.

also, even if it works, how can we transfer the data of whether the light is on or not to Arduino? (maybe phototransistor?)

 

3. hall effect proximity sensor

This sensor can detect metal, but if the shell is not metal how to detect the phone?

 

 

 


 

p5.js:

var hr, min, sec, inputh, inputm, alarmh, alarmm, button;
var bedtimeset = false;
var error = false;
var image;
let serial;
let portName='COM7';
let inData;
let score;
//let portSelector;
let options = {baudRate: 9600}; // change the data rate to whatever you wish
let onTime=0;

function preload() {
  hr = hour();
  min = minute();
  sec = second();
  img = loadImage('plant.png');
  img = loadImage('plant.png');
 
}

function setup() {
  createCanvas(windowWidth, windowHeight);
  serial = new p5.SerialPort();
  serial.on('list',printList);
  serial.list();
  serial.open(portName, options);
  serial.on('connected', serverConnected); // callback for connecting to the server
  serial.on('open', portOpen);        // callback for the port opening
  serial.on('data', serialEvent);     // callback for when new data arrives
  serial.on('error', serialError);    // callback for errors
  serial.on('close', portClose);      // callback for the por
 
  setInterval(addSec, 1000);// update current time
  inputh = createInput();
  inputh.position(windowWidth/2 - 200, windowHeight/2 + 100);
  inputm = createInput();
  inputm.position(windowWidth/2 + 50, windowHeight/2 + 100);
  button = createButton("Set Bedtime");
  button.style("font-size", "25px");
  button.position(windowWidth/2-80,4*windowHeight/5 + 10);
  button.mousePressed(checkInput);
  currentTime();
  var restButton=createButton('reset');
  restButton.style("font-size", "25px");
  restButton.position(10*windowWidth/11, windowHeight/50);
  restButton.mousePressed(reset);
}
function reset(){
  window.location.reload();
}

function draw() {
  background(255);
  image(img, 0, windowHeight / 2, img.width / 2, img.height / 2);
  image(img, windowWidth - 300, windowHeight / 2, img.width / 2, img.height / 2);
 

  fill(0);
  textAlign(CENTER);
  textSize(80);
  textStyle(BOLD);
  text(nf(hr, 2, 0) + ":" + nf(min, 2, 0) + ":" + nf(sec, 2, 0), windowWidth/2, windowHeight/2 - 50);
  textSize(20);
  fill(200);
  text("soil moisture: " +inData, 130, 50);
  text("sleeping score: " +score, 120, 100);
 
  //console.log(bedtimeset);
  if (bedtimeset == false){
    
    console.log(error);
    textAlign(CENTER);
  //text("Set Bedtime", windowWidth/2, 2*windowHeight/3);
  text(":", windowWidth/2, windowHeight/2 + 120);
  }
  if (bedtimeset == true){
    fill(0);
    textAlign(CENTER);
    textSize(30);
    text("Bedtime set to " + nf(alarmh, 2, 0) + ":" + nf(alarmm, 2, 0), windowWidth/2, 3*windowHeight/4);
}
  if (error == true){
    fill(255, 0, 0);
    textSize(30);
    text("error", windowWidth/2, windowHeight/2+20);

    textSize(10);
    text("Please type in valid hours and mintues", windowWidth/2, windowHeight/2 + 50);
  }
  bedTime();
}

//connect arduino
function printList(portList) {
  // portList is an array of serial port names
  for (var i = 0; i < portList.length; i++) {
    // Display the list the console:
    console.log(i + portList[i]);
  }
}

 

function serverConnected() {
  console.log('connected to server.');
}
 
function portOpen() {
  console.log('the serial port opened.')
}
 
function serialEvent() {
  var inString = serial.readStringUntil('\r\n');
  if (inString.length > 0 ) {
    var sensors = split(inString, ',');            // split the string on the commas
    if (sensors.length > 1) {                      // if there are three elements
    inData=Number(sensors[0]);
       score=Number(sensors[1]);
    }
  }      
}
 
function serialError(err) {
  console.log('Something went wrong with the serial port. ' + err);
}
 
function portClose() {
  console.log('The serial port closed.');
}


function addSec() {
  sec++;
  if (sec >= 60) {
    sec = 0;
    min++;
  }
  if (min >= 60) {
    mint = 0;
    hr++;
  }
  if (hr >= 24) {
    window.location.reload();
  }
}

function currentTime() {}

function checkInput() {
  // if error is true, that means that an error has already occurred, so when we press the button again, we want to set error to false
  if (error == true){
    error = false;
  }
 
  console.log("You pressed submit!")
  alarmh = int(inputh.value());
  alarmm = int(inputm.value());
  inputh.value("");
  inputm.value("");

  
  if (alarmh > 24 || alarmh < 0 || alarmm > 60 || alarmm < 0) {
    error = true;
    
  } else {
    clearCanvas();
    inputh.hide();
    inputm.hide();
    button.hide();
bedtimeset = true;
    console.log(
      "Bedtime set to " + nf(alarmh, 2, 0) + ":" + nf(alarmm, 2, 0) + "."
    );
  }
}

function clearCanvas() {
  rect(0, 0, canvas.width, canvas.height);
  background(255);
}

function bedTime() {
  outByte=onTime;
  serial.write(outByte);
  if (alarmh == hr && alarmm == min) {
    clearCanvas();
    fill(0);
    textAlign(CENTER);
    textSize(60);
    text("It is bedtime now!", windowWidth/2, windowHeight/2);
    onTime=1;
  }
  if(score>=1){
    onTime=0;
  }
}
 

Arduino:

int sensorPin=A0;
int sensorValue;
int buttonPin=2;
int buttonState=0;
int lastButtonState=0;
int pwmA=3;
int in1A=5;
int in2A=4;
int greenLed=6;
int yellowLed=8;
int redLed=9;
int onTime=0;
long tSleep;
long tPutPhone;
long t;
long tDuration;
int putScore=0;
int durationScore=0;
int score=0;
void setup() {
// pinMode(waterPin,OUTPUT);
pinMode(buttonPin,INPUT);
pinMode(7,OUTPUT);
pinMode(pwmA,OUTPUT);
pinMode(in1A,OUTPUT);
pinMode(in2A,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(yellowLed,OUTPUT);
pinMode(redLed,OUTPUT);
Serial.begin(9600);

}

void loop() {
   timeCounts();
   digitalWrite(in1A,HIGH);
  digitalWrite(in2A,LOW);
  sensorValue=analogRead(A0);
  remindWatering(sensorValue);
  analogWrite(pwmA,0);
  scoresystem(tSleep,tPutPhone,t);
}


void timeCounts(){
  onTime = Serial.read(); // read it
  buttonState=digitalRead(buttonPin);
  if(buttonState!=lastButtonState){
  if(buttonState==HIGH){
      //buttonClick,phone put down
      tPutPhone=millis(); 
     
    }
    //buttonOpen
    if(buttonState==LOW){
    t=(millis()-tPutPhone)/1000;
    }  
  }
  lastButtonState=buttonState;
  if(onTime==1){
    long tSleep=millis();
    digitalWrite(7,HIGH);
  }
  if(t<2&&buttonState==LOW&&tSleep!=0){
  digitalWrite(7,HIGH);
}else if(buttonState==HIGH){
  digitalWrite(7,LOW);
}else if(t>=2||onTime==0){
  digitalWrite(7,LOW);
}
}

void scoresystem(long a,long b,long c){
  if(c>=2){
     int putScore=5-(b-a)/2000;
     putScore=constrain(putScore,0,5);
  tDuration=c/2;
  if(tDuration>7){
    durationScore=5;
  }else{
    durationScore=5+(tDuration-5);
  }durationScore=constrain(durationScore,0,5);
  score=putScore+durationScore;
  }else{score=0;}
  Serial.print(sensorValue);
  Serial.print(",");
  Serial.println(score);
    if(score>=1){
    analogWrite(pwmA,78);
    delay(score*1000);
    tPutPhone=0;
    t=0;
    score=0;
    tSleep=0;
    tDuration=0;
    }
}
void remindWatering(int sensorValue){
  if(sensorValue<300){
    digitalWrite(greenLed,HIGH);
    digitalWrite(yellowLed,LOW);
    digitalWrite(redLed,LOW);
  }else if(sensorValue<500){
    digitalWrite(yellowLed,HIGH);
    digitalWrite(greenLed,LOW);
    digitalWrite(redLed,LOW);
  }else if(sensorValue>500){
    digitalWrite(redLed,HIGH);
    digitalWrite(yellowLed,LOW);
    digitalWrite(greenLed,LOW);
  }
}

bottom of page