Android : Brightness of led

on Friday, October 10, 2014


I am doing a project involving android and arduino. My project has two leds, with one controlling the brightness and the other just on and off the led. I'm having problems in the arduino part for compiling my brightness program with the TurnOn/OFF led program. Any suggestions on what I should do?


Android Program for brightness:



sr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
srValue = (byte) arg1;

}

@Override
public void onStartTrackingTouch(SeekBar arg0) {


}

@Override
public void onStopTrackingTouch(SeekBar arg0) {

String temp = "r";
byte bytes1[] = temp.getBytes();
try {
outStream.write(bytes1);
} catch (IOException e) {
e.printStackTrace();
}

try {
outStream.write(srValue);
} catch (IOException e) {
e.printStackTrace();
}

}});

}


Android program for ON/OFF Led:



tg2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if((tg2.isChecked()))
{
System.out.println("checked");
tv2.setBackgroundColor(0xFF00FF00);
sendData("1");
}
else
{
System.out.println("Unchecked");
tv2.setBackgroundColor(0xfff00000);
sendData("4");
}
}
});


Arduino Program for brightness:



byte packet[2];
int pIndex;
int rPin = 9;

byte rValue = 0;

void setup() {
Serial.begin(9600);
pinMode(rPin, OUTPUT);
analogWrite(rPin, 0);
}

void loop() {

// see if there's incoming serial data:
if (Serial.available() > 0) {
packet[pIndex++] = Serial.read();
}

if(pIndex >= 2){

switch(packet[0]){
case 't':
rValue = packet[0];
break;
case 'y':
rValue = packet[3];
break;
case 'r':
rValue = packet[1];
break;
default:
;
}

analogWrite(rPin, rValue); // 0 - 255

pIndex = 0;

}

}


Arduino Program for Turn ON/OFF:



if (state == '1') // ON LED 2
{
analogWrite(ledPin2, 255);
}
else if(state == '4')
{
analogWrite(ledPin2,0);
}

0 comments:

Post a Comment