Using an arduino Mega to collect temperature and humidity data then using Processing to display data on meters. Everything works fine for a few seconds then I receive this error: ArrayIndexOutOfBoundsException: 1. Not sure why?
Arduino Code:
#include <Wire.h>
#include "Wire.h"
#include "Adafruit_HTU21DF.h"
#include <SPI.h>
#include <SoftwareSerial.h>
extern "C" {
#include "utility/twi.h" // from Wire library, so we can do bus scanning
}
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
#define TCAADDR 0x70 // mux 1 #define TCAADDR 0x70 // mux 1
#define TCAADDR2 0x71 // mux 2 #define TCAADDR 0x71 // mux 2
#define TCAADDR3 0x72 // mux 3 #define TCAADDR 0x72 // mux 3
#define TCAADDR4 0x73 // mux 4 #define TCAADDR 0x73 // mux 4
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
// array of temperature designated by [ ] when calling call from 0-9 for an array of size 10
float temperature[3]= {-1, -1, -1};
float humidity[3]= {-1, -1, -1};
float humiditymax[10]={60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0};
float humiditymin[10]={24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 24.0};
//temperature max is 100F and min is 60F
float temperaturemax[10]={37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8};
float temperaturemin[10]={15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55};
//temp below 60 above 100
void setup() {
// put your setup code here, to run once:
Wire.begin(); // Initiate the Wire library
Serial.begin(9600);
tcaselect(7);
htu.begin();
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println("in loop");
///////////////send data from sensors to the lath house , main room, green house 1 and 2 ////////////
// Ex. tcaselect(1) is option 1 temperature/humidity reading, tcaselect(2) is option 2 temperature/humidity reading..etc
//total 10 options/readings, mux1 reads from sensors 0-3, mux2 reads from sensors 4-7, mux3 reads from sensors 8 & 9
//mux1
tcaselect(1);
temperature[0] = htu.readTemperature();
humidity[0] = htu.readHumidity();
tcaselect(2);
temperature[1] = htu.readTemperature();
humidity[1] = htu.readHumidity();
tcaselect(6);
temperature[2] = htu.readTemperature();
humidity[2] = htu.readHumidity();
Serial.print((int)temperature[0]);
Serial.print(",");
Serial.println((int)temperature[1]);
delay(2500);
}
void tcaselect(uint8_t i) {
// set the mux we want to the correct loacation// reading from mux1, disable other muxes
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
//disable all other muxes
Wire.beginTransmission(TCAADDR2);
Wire.write(0); // no channel selected
Wire.endTransmission();
Wire.beginTransmission(TCAADDR3);
Wire.write(0); // no channel selected
Wire.endTransmission();
Wire.beginTransmission(TCAADDR4);
Wire.write(0); // no channel selected
Wire.endTransmission();
}
Processing Code:
/***********************************
First upload the Arduino code to the Arduino and then run this code.
Make sure that the Port number and baudrate are the same.
***********************************/
import meter.*;
import processing.serial.*;
Serial port;
String[] list;
Meter m, m2;
void setup() {
size(950, 400);
background(0);
port = new Serial(this, "/dev/ttyACM0", 9600);
fill(120, 50, 0);
m = new Meter(this, 25, 100);
// Adjust font color of meter value
m.setTitleFontSize(20);
m.setTitleFontName("Arial bold");
m.setTitle("Temperature (C)");
m.setDisplayDigitalMeterValue(true);
// Meter Scale
String[] scaleLabelsT = {"0", "10", "20", "30", "40", "50", "60", "70", "80"};
m.setScaleLabels(scaleLabelsT);
m.setScaleFontSize(18);
m.setScaleFontName("Times New Roman bold");
m.setScaleFontColor(color(200, 30, 70));
m.setArcColor(color(141, 113, 178));
m.setArcThickness(10);
m.setMaxScaleValue(80);
m.setNeedleThickness(3);
m.setMinInputSignal(0);
m.setMaxInputSignal(80);
// A second meter for reference
int mx = m.getMeterX();
int my = m.getMeterY();
int mw = m.getMeterWidth();
m2 = new Meter(this, mx + mw + 20, my);
m2.setTitleFontSize(20);
m2.setTitleFontName("Arial bold");
m2.setTitle("Humidity (%)");
m2.setDisplayDigitalMeterValue(true);
String[] scaleLabelsH = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m2.setScaleLabels(scaleLabelsH);
m2.setScaleFontSize(18);
m2.setScaleFontName("Times New Roman bold");
m2.setScaleFontColor(color(200, 30, 70));
m2.setArcColor(color(141, 113, 178));
m2.setArcThickness(10);
m2.setMaxScaleValue(100);
m2.setNeedleThickness(3);
m2.setMinInputSignal(0);
m2.setMaxInputSignal(100);
}
public void draw() {
textSize(30);
fill(0, 255, 0);
text("Temperature and Humidity", 270, 50);
if (port.available() > 0) {
String val = port.readString();
list = split(val, ',');
float temp = float(list[0]);
float hum = float(list[1]);
println("Temperature: " + temp + " C " + "Humidity: " + hum + " %");
m.updateMeter(int(temp));
m2.updateMeter(int(hum));
delay(2500);
}
}