Week 14: Final Project Progress 2

This week we (Han Seoyeong & Lina Storfors) have focused on getting the two signals (moisture and light) into Processing and to be able to control them seperately.  We managed to do it successfully by using the code for sending seperate signals into Processing that we learned in class during week 9, and modified it to suit our needs (a moisture sensor and a light sensor instead of the potentiometer and button in the original code).
This is how it worked:


Arduino code:
int lightPin = A0;
int moisturePin = A1;

const int averageCount = 16;
float averageBuffer[averageCount];

void setup() {
Serial.begin (9600);
//pinMode(buttonPin, INPUT); // this is for digital pin only? or buttons only?
}

void loop() {
int lightVal = analogRead(lightPin);
int moistureVal = analogRead(moisturePin);

Serial.print(lightVal);
Serial.print(","); // use comma as delimiter/separator
Serial.print(moistureVal);
Serial.println();

delay(50);
}


Processing code:
import processing.serial.*;
Serial myPort;
int ligVal = 0;
int moistVal = 0;

void setup()
{
size(800,500);
printArray(Serial.list());
String portname = Serial.list()[0]; // <- change the number according to your port setup
myPort = new Serial(this, portname, 9600);

myPort.bufferUntil('\n');
}

void draw()
{
background(0);

if (moistVal < 100) fill(255,0,0); // if moisture number is less than hundred, fill with red, otherwise white
else fill(255);

float eSize = ligVal / 5;
ellipse(width/2, height/2, eSize, eSize); // the ellipse changes size depending on the light

}

void serialEvent(Serial port)
{
String myString = myPort.readStringUntil('\n');
if (myString != null) {
// remove whitespaces (spacebar)
String trimmed = trim(myString);

// separate signals into multiple values with delimiter(comma)
String[] vals = split(trimmed, ',');

// makes sure we received all 2 values
if (vals != null && vals.length == 2){
println(vals[0] + " " + vals[1]);
// 1st value is light value
ligVal = int(vals[0]);
// 2nd valus is moisture value
moistVal = int(vals[1]);
}
}
}


IR distance sensor

We also tried the IR distance sensor as a possible replacement for the proximity sensor. After googling around we found a code that was fairly simple, just to see how the sensor worked.
This is our result:




Arduino code:
// from http://www.learningaboutelectronics.com/Articles/GP2Y0A21-Infrared-IR-Distance-Sensor-Circuit.php

const int signalPin= 0; //yellow wire connects to analog pin A0
const int LEDPin= 13; //LED connects to digital pin 13
int signal;//this variable, signal, will hold the analog value read by the arduino

void setup() {
Serial.begin(9600); //sets the baud rate for data transfer in bits/second
pinMode(signalPin, INPUT); //the infrared sensor signal line will be an input to the arduino
pinMode(LEDPin, OUTPUT); //the LED serves an output in the circuit
}

void loop() {
signal= analogRead(signalPin); //arduino reads the value from the infrared sensor
Serial.println(signal); //prints out analog value
delay(500); //delays the next analog reading by 500 ms or a half a second

if(signal < 200){ //if the analog value is less than 200, the object is within a few inches
digitalWrite(LEDPin, HIGH);
}
else{
digitalWrite(LEDPin, LOW);
}
}


It seems that the IR distance sensor is more reliable than the proximity sensor that we used before, and could be a good replacement. However, since the professor has advised us that using more than two signals is difficult and can make the signals disrupt each other, we decided to complete the other parts of the project that is the most essential first. If we have time, we can try to include the IR distance sensor and see if we can make it work later.

Design of final project:

We have also started working on the processing design of our final project. This is how it looks so far:




Processing code:
import processing.serial.*;
Serial myPort;
int ligVal = 0;
int moistVal = 0;

void setup()
{
size(1800,1000);
printArray(Serial.list());
String portname = Serial.list()[0]; // <- change the number according to your port setup
myPort = new Serial(this, portname, 9600);

myPort.bufferUntil('\n');
}

void draw()
{
background(255,216,216);
PImage thirsty;
PImage good;
PImage perfect;
PImage dark;
PImage good2;
PImage perfect2;

/*fiddle leaf fig (tree), orchid(flowers) , cactus

tree :
flowers :
cactus :

*/


stroke(255);
strokeWeight(5);
noFill();
rect(50,100,1700,800);

//about moisture sensor
strokeWeight(3);
line(300,300,700,300);
if (650 < moistVal && moistVal <= 1023){
  thirsty = loadImage("thirsty.PNG");
  thirsty.resize(200,300);
  image(thirsty,220,160);
}  else if(450 < moistVal && moistVal <= 650){
  good = loadImage("good.PNG");
  good.resize(200,300);
  image(good,410,160);
} else
 {perfect = loadImage("perfect.PNG");
  perfect.resize(200,300);
  image(perfect,600,160);
}

//about light sensor
stroke(255);
line(300,500,700,500);
if (900 < ligVal && ligVal <= 1023){
  dark = loadImage("dark.PNG");
  dark.resize(200,300);
  image(dark,220,350);
}  else if(600 < ligVal && ligVal <= 900){
  good2 = loadImage("good2.PNG");
  good2.resize(200,300);
  image(good2,400,350);
} else
 {perfect2 = loadImage("perfect2.PNG");
  perfect2.resize(200,300);
  image(perfect2,600,350);
}



}

void serialEvent(Serial port)
{
String myString = myPort.readStringUntil('\n');
if (myString != null) {
// remove whitespaces (spacebar)
String trimmed = trim(myString);

// separate signals into multiple values with delimiter(comma)
String[] vals = split(trimmed, ',');

// makes sure we received all 2 values
if (vals != null && vals.length == 2){
println(vals[0] + " " + vals[1]);
// 1st value is light value
ligVal = int(vals[0]);
// 2nd valus is moisture value
moistVal = int(vals[1]);
}
}
}

Kommentarer

Populära inlägg