We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I'm new to this whole processing thing, been learning some basic stuff for this particular assignment. How ever I'm not sure if I should make some changes to my Arduino code to make this assignment easier.
This is the task I've got: Using Processing, build a GUI for the system that you built for Tasks 1, 2 and 3. Your Arduino program might need to be adapted.
-You should built a user interface to implement all the functions of the previous tasks. -The physical push-buttons should still work, but their functions should be also present in the GUI. -Depending on where the user clicks on the screen, your program should send the appropriate character to Arduino. -The values returned by the Arduino should be printed in the Processing console and also indicated using a graphical scale of your choice (bar graph, scope like plot, etc.).
Here is the code for Arduino:
#include <Servo.h> // include Servo library
Servo myServo;
int sensorValueTemp;
int sensorValueHum;
int heartBeat = 0;
int blinkingMIN = 0;
int blinkingMAX = 0;
int angle1; // angle to be rotated by servo when sensing temperature
int angle2; // angle to be ratated by servo when sensing humidity
int selectedSensor = 2;
int leds = 2;
int reading1;
int reading2;
boolean continuousRead = false;
const int humiditySensor = A1; // humidity sensor in A1 pin
const int tempSensor = A0; // temperature sensor in AO pin
const int button1 = 11; // push-button for changing which sensor is active
const int button2 = 10; // push-botton for switching the digital ledscale ON/OFF
const int led = 12; // which sensor is reading; ON=temperature, OFF=humidity
float temperature;
float voltage;
float sensorRH;
float trueRH;
const float roomTemp = 23;
unsigned long timer1;
unsigned long timer2;
unsigned long timer3;
unsigned long timer4;
unsigned long timer5;
void setup() {
myServo.attach(9,480,2240); // (servo attached to pin 9, minimum pulse, servo maximum pulse)
Serial.begin(9600); // open a serial connection to display values
Serial.setTimeout(10);
// set the LED button and sensors as inputs
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(tempSensor, INPUT);
pinMode(humiditySensor, INPUT);
// set the LED pins as outputs
pinMode(13, OUTPUT);
pinMode(led, OUTPUT);
for (int pinNumber = 2; pinNumber < 8; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
//LED test at startup
digitalWrite(2,HIGH);
delay(100);
digitalWrite(3,HIGH);
delay(100);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(6,HIGH);
delay(100);
digitalWrite(7,HIGH);
delay(500);
for (int pinNumber = 2; pinNumber < 8; pinNumber++) {
digitalWrite(pinNumber, LOW);
}
delay(500);
}
void ledScale() {
// if the current temperature is lower than the roomTemp: turn off all LEDs and blink the LED in pin 2 -> temperature under minimum value
if (temperature < roomTemp +2) {
for (int pinNumber = 3; pinNumber < 8; pinNumber++) {
digitalWrite(pinNumber, LOW);
// Make the LED blink
if (millis() - timer1 > 500){
blinkingMIN = !blinkingMIN;
digitalWrite (2, blinkingMIN);
timer1 = millis();
}
}
}
// if the temperature rises 2-3 degrees: turn an LED on
else if (temperature >= roomTemp + 2 && temperature < roomTemp + 3) {
digitalWrite(2, HIGH);
for (int pinNumber = 3; pinNumber < 8; pinNumber++) {
digitalWrite(pinNumber, LOW);
}
}
// if the temperature rises 3-4 degrees: turn a second LED on
else if (temperature >= roomTemp + 3 && temperature < roomTemp + 4) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
for (int pinNumber = 4; pinNumber < 8; pinNumber++) {
digitalWrite(pinNumber, LOW);
}
}
// if the temperature rises 4-5 degrees: turn a third LED on
else if (temperature >= roomTemp + 4 && temperature < roomTemp + 5) {
for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
digitalWrite(pinNumber, HIGH);
for (int pinNumber = 5; pinNumber < 8; pinNumber++) {
digitalWrite(pinNumber, LOW);
}
}
}
// if the temperature rises 5-6 degrees: turn a fourth LED on
else if (temperature >= roomTemp + 5 && temperature < roomTemp + 6) {
for (int pinNumber = 2; pinNumber < 6; pinNumber++) {
digitalWrite(pinNumber, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
}
// if the temperature rises 6-7 degrees, turn a fifth LED on
else if (temperature >= roomTemp + 6 && temperature < roomTemp + 7) {
for (int pinNumber = 2; pinNumber < 7; pinNumber++) {
digitalWrite(pinNumber, HIGH);
digitalWrite(7, LOW);
}
}
// if the temperature goes 7 degrees over the roomTemp: turn on all LEDs and blink the led in pin 7 -> maximum value has been crossed
else if (temperature >= roomTemp + 7) {
for (int pinNumber = 2; pinNumber < 7; pinNumber++) {
digitalWrite(pinNumber, HIGH);
// Make the LED blink
if (millis() - timer2 > 500) {
blinkingMAX = !blinkingMAX;
digitalWrite (7, blinkingMAX);
timer2 = millis();
}
}
}
}
void sendMeasurements() {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print(trueRH);
Serial.println("%");
}
void toggleSensor() {
selectedSensor++;
if(selectedSensor > 2)
selectedSensor = 1;
}
void toggleLeds() {
leds++;
if(leds > 2)
leds = 1;
}
void parseCommands(char input) {
switch (input) {
case 'L':
case 'l':
if (leds == 2){
leds = 1;
}
else{
leds = 2;
}
break;
case 'M':
case 'm':
sendMeasurements();
break;
case 'C':
case 'c':
continuousRead = !continuousRead;
break;
case 'S':
case 's':
toggleSensor();
break;
case 'K':
case 'k':
Serial.print("Selected sensor: ");
switch (selectedSensor){
case 1:
Serial.println("Temperature");
break;
case 2:
Serial.println("Humidity");
break;
}
Serial.print("Leds ON/OFF: ");
switch (leds){
case 1:
Serial.println("OFF");
break;
case 2:
Serial.println("ON");
break;
}
break;
}
}
void loop() {
// Make the LED blink in pin 13 for "heart beat"
if (millis() - timer3 > 500){
heartBeat = !heartBeat;
digitalWrite (13, heartBeat);
timer3 = millis();
}
// Button 1 to toggle sensor
reading1 = digitalRead(button1);
if(digitalRead(button1)) {
toggleSensor();
}
// Read the temperature sensor values
analogRead(tempSensor);
// Timer for updating the sensor values at 1Hz
if(millis() - timer4 > 1000){
sensorValueTemp = analogRead(tempSensor);
temperature = (5.0 * sensorValueTemp * 100.0) / 1024;
// Mapping the servo for temperature sensor
angle1 = map(temperature, 10, 50, 0, 180);
// Read the humidity sensor values
sensorValueHum = analogRead(humiditySensor);
voltage = (5.0 * sensorValueHum) / 1024.0;
sensorRH = (voltage - 0.958) / 0.03068;
trueRH = (sensorRH) / (1.0546 - 0.00216 * 23);
// Mapping the servo for humidity sensor
angle2 = map(trueRH, 40, 80, 0, 180);
// Switchcase for button 1 to select which sensor is active
switch(selectedSensor) {
case 1:
digitalWrite(led, HIGH);
myServo.write(angle1);
break;
case 2:
digitalWrite(led, LOW);
myServo.write(angle2);
break;
}
timer4 = millis();
}
// Reading the parse commands
char command = Serial.read();
while(command != -1) {
parseCommands(command);
command = Serial.read();
}
// Timer for seding the sensor values to serial monitor once a second
if(millis() - timer5 > 1000 && continuousRead) {
sendMeasurements();
timer5 = millis();
}
// Button 2 to toggle leds
reading2 = digitalRead(button2);
if(digitalRead(button2)) {
toggleLeds();
}
// Switchcase for button 2 to switch ON/OFF the digital ledScale
switch(leds) {
case 1:
for (int pinNumber = 2; pinNumber < 8; pinNumber++) {
digitalWrite(pinNumber, LOW);
}
break;
case 2:
ledScale();
break;
}
}
Any help will be appreciated,
Jonathan.
Answers
Jonathan,
You could make Processing buttons that send the commands that you are currently using. Then make Processing code that reads what the Arduino is printing out. However it's more systematic to use something that transfers values both ways. Initially this will seem like more work to start with, but will be more flexible and managable in the end.
You'll almost certainly have to remove your current serial code - difficult to have two different schemes running on one link.
If you look on the Processing libraries web-page, there are two libraries that sync Processing and Arduino variables. Look for AP-Sync or VSync. I didn't discover these until after I'd made my own (mostly copied from someone else's).
I'm happy to give you my code, but I'll let you look at those first, they are probably better developed and documented.
Richard.