Hi guys, I’m making a simple app to turn ON/OFF relays connect with lamps, and in Android I have button to do this.
Ideally, for example, if I turn ON (Lamp 1) and OFF (Lamp 2), when I close and reopen the Android app, it will update the relay state by the color/text of the buttons
However, I’m stucking on it, all mess up
Here is the code
For Photon :
int light1 = D0;
int light2 = A0;
int state1 = digitalRead(D0);
int state2 = digitalRead(A0);
int i = -1;
String cmd[] = {"ON_1", "OFF_1", "ON_2", "OFF_2", "ON_ALL", "OFF_ALL"};
int status ;
void setup() {
//status = 1;
pinMode(light1, OUTPUT);
digitalWrite(light1,HIGH);
pinMode(light2, OUTPUT);
digitalWrite(light2,HIGH);
Particle.function("on-off", toggleSwitch);
Particle.variable("lightstate", status);
}
void loop() {
if (state1 == LOW && state2 == LOW)
{
status = 0;
}
if (state1 == LOW && state2 == HIGH)
{
status = 1;
}
if (state1 == HIGH && state2 == LOW)
{
status = 2;
}
if (state1 == HIGH && state2 == HIGH)
{
status = 3;
}
}
int toggleSwitch(String command) {
String convertedCommand = command.toUpperCase();
for (i = 0; i <= sizeof(cmd)/sizeof(cmd[0]); i++) // notice the less than or equal to comparison
{
if (convertedCommand.equals(cmd[i])) // compare String myCommand and Array Element
break;
}
switch(i) {
case 0:
digitalWrite(light1,LOW);
break;
case 1:
digitalWrite(light1,HIGH);
break;
case 2:
digitalWrite(light2,LOW);
break;
case 3:
digitalWrite(light2,HIGH);
break;
case 4:
digitalWrite(light1,LOW);
delay(200);
digitalWrite(light2,LOW);
break;
case 5:
digitalWrite(light1,HIGH);
delay(200);
digitalWrite(light2,HIGH);
break;
}
}
Android code :
Async.executeAsync(ParticleCloud.get(RemoteControlActivity.this), new Async.ApiWork<ParticleCloud, Integer>() {
private ParticleDevice mDevice;
@Override
public Integer callApi(ParticleCloud particleCloud) throws ParticleCloudException, IOException {
mDevice = particleCloud.getDevice(getIntent().getStringExtra(ARG_DEVICEID));
int status = 0;
try {
status = mDevice.getIntVariable("lightstate");
} catch (ParticleDevice.VariableDoesNotExistException e) {
e.printStackTrace();
}
return status;
}
public void onSuccess(Integer result) {
Log.d(TAG,"Lightstate :" + String.valueOf(result));
if (Integer.valueOf(result)== 0)
{
sw1.setText("ON");
sw1.setBackgroundColor(Color.YELLOW);
sw2.setText("ON");
sw2.setBackgroundColor(Color.YELLOW);
btnall_on.setVisibility(View.INVISIBLE);
btnall_off.setVisibility(View.VISIBLE);
}
if (Integer.valueOf(result)== 1)
{
sw1.setText("ON");
sw1.setBackgroundColor(Color.YELLOW);
sw2.setText("OFF");
sw2.setBackgroundColor(Color.GRAY);
btnall_on.setVisibility(View.VISIBLE);
btnall_off.setVisibility(View.VISIBLE);
}
if (Integer.valueOf(result)== 2)
{
sw1.setText("OFF");
sw1.setBackgroundColor(Color.GRAY);
sw2.setText("ON");
sw2.setBackgroundColor(Color.YELLOW);
btnall_on.setVisibility(View.VISIBLE);
btnall_off.setVisibility(View.VISIBLE);
}
if (Integer.valueOf(result)== 3)
{
sw1.setText("OFF");
sw1.setBackgroundColor(Color.GRAY);
sw2.setText("OFF");
sw2.setBackgroundColor(Color.GRAY);
btnall_on.setVisibility(View.VISIBLE);
btnall_off.setVisibility(View.INVISIBLE);
}
}
public void onFailure(ParticleCloudException value) {
}
});
When I test on Android App, I always get the lightstate : 3
Plese kindly check and explain to me :
-
Can we minimize the length of the code, I mean the algorithm or formula I used is quite long and complicated
-
How about in the future, if I control over 3, 4 or event 10 devices, can I do like this, if so, it will be a very big lines of code
Thank you for your understanding !