I am trying to control multiple LEDs but I am not quite sure on how to adjust my code accordingly. I have a master code that runs an RTC clock module and a light sensor to an LED. I have separated both my codes. I was able to control an LED simply by connecting it to ground and pin 9 on an Arduino board. After running processing for just that, I was able to turn the LED on/off. Once I tried to combine it with my master code, I believe the problem was that I am using
port = new Serial(this, "/dev/cu.usbmodem14331", 9600);
and
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.bufferUntil(lf);
delay(1000);
(this is for the sensor)
together.
Below is the Processing code for only the on/off buttons.
import controlP5.*; //library
import processing.serial.*; //library
Serial port; //do not change
ControlP5 cp5; //create ControlP5 object
PFont font; //do not change
void setup() { //same as arduino program
size(300, 300); //window size, (width, height)
port = new Serial(this, "/dev/cu.usbmodem14331", 9600); //connected arduino port
cp5 = new ControlP5(this); //do not change
font = createFont("Georgia Bold", 20); //font for buttons and title
cp5.addButton("ON") //name of button
.setPosition(95, 50) //x and y upper left corner of button
.setSize(120, 70) //(width, height)
.setFont(font) //font`
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0)) //text color r,g,b
;
cp5.addButton("OFF")
.setPosition(95, 150)
.setSize(120, 70)
.setFont(font)
.setColorBackground(color(255, 0, 0))
.setColorForeground(color(0, 255, 0))
.setColorLabel(color(0, 0, 0))
;
}
void draw() {
background(0, 0, 0); // background color of window (r, g, b)
}
void ON() {
port.write(1);
}
void OFF() {
port.write(2);
}
Below is the Arduino code for only the on/off button:
void setup() {
pinMode(9, OUTPUT); //set pin as output , red led
Serial.begin(9600); //start serial
}
void loop(){
if(Serial.available()){ //id data available
int val = Serial.read();
if(val == 1){ //if 1 received
digitalWrite(9, HIGH); //turn on
}
if(val == 2){ //if 2 received
digitalWrite(9, LOW); //turn off
}
}
}
What I am trying to do is to combine these codes into my master, but I am still getting errors with the port.
Here is my master code for Processing:
import controlP5.*; //import ControlPS library
import processing.serial.*;
Serial port;
Serial myPort; //serial port
String inString; //input string from serial port
int lf = 10; //ASCII linefeed
ControlP5 cp5; //create ControlPS object
PFont font;
void setup(){
size(600,800); //window size, (width, height)
//adding buttons
cp5 = new ControlP5(this);
font = createFont("calibri light", 14); //fonts for buttons and title
cp5.addButton("Room 1 ON")
.setPosition(50, 50) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont (font)
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Room 1 OFF")
.setPosition(180, 50) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont (font)
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Room 2 ON")
.setPosition(50, 200) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont (font)
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Room 2 OFF")
.setPosition(180, 200) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont (font)
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0))
; cp5.addButton("Room 3 ON")
.setPosition(50, 350) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont (font)
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Room 3 OFF")
.setPosition(180, 350) //x and y coordinates of upper left corner of button
.setSize(120, 70) //(width, height)
.setFont (font)
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0))
;
// List all the available serial ports:
printArray(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.bufferUntil(lf);
delay(1000);
myPort = new Serial(this, Serial.list()[2], 9600);
myPort.bufferUntil(lf);
delay(1000);
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil(lf);
delay(1000);
;
cp5.addButton("Errors")
.setPosition(60, 500)
.setSize(125, 100)
.setFont (font)
;
cp5.addButton("Alarms")
.setPosition(60, 650)
.setSize(125, 100)
.setFont (font)
;
}
void draw(){
background(0, 0, 0); //background color of window
//text
fill(0, 255, 0); // (r, g, b) or (0 to 255)
text("CONTROL CENTER", 250, 30);
background(0);
text("Sensor Reading 1: " + inString, 325,100);
text("Sensor Reading 2: " + inString, 325,250); //will need to revise once 3 sensors connected
text("Sensor Reading 3: " + inString, 325,400);
}
void ON() {
port.write(1);
}
void OFF() {
port.write(2);
}
void serialEvent(Serial p)
{
inString = p.readString();
}
Below is my master Arduino code:
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <SparkFunDS1307RTC.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h" // the photoresistor module
const int led = 9;
int brightness = 255; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int targetbrightness = 255; // target brightness
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
//configure TSL2591
void configureSensor(void)
{
tsl.setGain(TSL2591_GAIN_MED);
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
Serial.println(F("------------------------------------"));
Serial.print (F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print (F("Timing: "));
Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("------------------------------------"));
}
void setup() {
// prepare pin as output
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
rtc.autoTime();
// wait for Arduino Serial Monitor
while (!Serial) ;
// get and set the time from the RTC
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// To test your project, we can set the time manually by uncommenting bottom line
//setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms, to trigger functions at specific times
Alarm.alarmRepeat(6,00,0,MorningAlarm); // 9:00am every day
Alarm.alarmRepeat(19,00,0,EveningAlarm); // 19:00 -> 7:00pm every day
// TSL2591
Serial.println(F("Starting Adafruit TSL2591..."));
if (tsl.begin())
{
Serial.println(F("Found a TSL2591 sensor"));
}
else
{
Serial.println(F("No sensor found ..."));
while (1);
}
configureSensor();
}
// The first "flag" for checking the Lux levels. If the Lux level is less than 500 then luxCheck will be 0.
int luxCheck;
// Reads IR and full spectrum then converts to Lux and checks for less than 500 Lux value
void advancedRead(void)
{
sensors_event_t event;
tsl.getEvent(&event);
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
Serial.println(tsl.calculateLux(full, ir), 6);
if ((event.light < 500))
{
luxCheck = 0;
Serial.println(F("WARNING: LUX NOT WITHIN DESIRED RANGE!"));
Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 2);
}
else
{
luxCheck = 1;
Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 2);
}
}
void loop() {
analogWrite(led, brightness);
if (hour() >= 6 && hour()<= 11) {
brightness = brightness + fadeAmount/18000;
}
else if (hour() > 11 && hour() <= 19) {
brightness = brightness - fadeAmount/28800;
}
else { brightness = 0;
}
// wait one second between each clock display in serial monitor
Alarm.delay(1000);
advancedRead();
}
// functions to be called when an alarm triggers
void MorningAlarm() {
// write here the task to perform every morning
Serial.println("Turn light on");
digitalWrite(led, HIGH);
}
void EveningAlarm() {
// write here the task to perform every evening
Serial.println("Turn light off");
digitalWrite(led, LOW);
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
Please let me know if you have any suggestions that will help me. Thank you in advance.