While loop in Android studio (button) - android

I have a button which is let users when open the application then can use the function.
This is the function:
if(eyeDected)
{
if(detectedFrame > 25)
{
eyeDected = false;
detectedFrame = 0;
finish();
} else {
detectedFrame++;
Log.d("UNLOCK:", String.valueOf(detectedFrame));
}
} else {
eyeDected = true;
detectedFrame++;
Log.d("UNLOCK:", String.valueOf(detectedFrame));
}
I want do a while loop function: (the number of click is a,b,c) (For example b=1 is mean that user click once time only)
a=0
b=1
c=2
when 'a' = 0,3,6,9,12,15..... (mean 'a'+3) then run the function
when 'b' = 1,4,7,10 ,13....(mean b+3) then run the function
when 'c' = 2,5,8,11,16... (mean b+3) then run the function
How can i perform a while loop for these? Thanks

You can add following condition :
a=0
b=1
c=2
when 'a' = 0,3,6,9,12 (mean 'a'+3) then run a function
if(a%3 == 0){
// run a function
}
when 'b' = 1,4,7,10 (mean b+3) then run a function
if((b+1)%3 == 0){
// run B function
}
when 'c' = 2,5,8,11 (mean b+3) then run a function
if((c+2)%3 == 0){
// run B function
}
Ex. in loop :
for (int i=0;i<100;i++){
if( i % 3 == 0){
// run a function
}
else if( (i+1) % 3 == 0){
// run b function
}
else if( (i+2) % 3 == 0){
// run c function
}
}

Related

How can I send two values from bluetoothSPP in android to arduino?

This is my code.
I wanna receive two values in Arduino from android app.
I can receive one value but I can't two values.
Here is my code.
I wanna send two values with Bluetooth.send().
How can I do that?
I use bluetoothSPP in android studio.
I wanna receive Go ,Back, Right, Left and seconds in arduino.
so If i receive Go and 2sec, my arduino rc car go for 2 sec.
//Arduino code
#include<SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial BT(blueTx,blueRx);
Servo servo;
char input1;
char input2;
void loop() {
if(BT.available()){
if(Serial.available()){
Serial.println("------");
Serial.println(BT.read());
}
input1 = BT.read();
input2 = BT.read();
}
}
// Android code , i use bluetoothSPP and send method
blockStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!list.isEmpty()) {
delayHandler.postDelayed(new Runnable() {
#Override
public void run() {
while(!list.isEmpty()) {
String blockName = list.get(0);
num1 = inputNum.getText().toString();
list.remove(0);
adapter.notifyDataSetChanged();
if (blockName == "GO") {
Bluetooth.send("G", true);
Bluetooth.send(num1, true);
} else if (blockName == "BACK") {
Bluetooth.send("K", true);
Bluetooth.send(num1, true);
} else if (blockName == "LEFT") {
Bluetooth.send("L", true);
Bluetooth.send(num1, true);
} else if (blockName == "RIGHT") {
Bluetooth.send("R", true);
Bluetooth.send(num1, true);
}
// inputNum.setText(null);
}
}
}, 10);
}
I had similar problem in the problem , though I did'nt use bluetooth spp , but give this a shot , try to send byte array in send
I designed my input like a,b and convert this to byte array , in your case send it like G/K/L/R,num1 see the comma added , to convert this to byte array use getBytes() , in aurdino do the following :
void loop() {
if(Serial.available()){
int available = Serial.available();
for(int i=0; i< available; i++){
char a = Serial.read();
if(i < 1){
input1 = a;
}
if(i > 1){
int c =(int)a - 48;
input2 *= 10;
input2 += c;
}
}
}
Explanation skip comma which is second character(1) , first character is normal so add it to input1 , second one is number so extract ascii values starts from 48 and get the number from it.Input2 is of type int with initial value of 0 , Enjoy!

How can i create a date and time picker as in the below image

i need some lead on how i could develop time slot picker as show in the below screen shot.
Screen shot
I'd store the opening times in some DB Table and fetch them via ajax, then you can use some js library like moment.js for getting "humanized" words from your times (eg. Morning, Afternoon, Evening etc.).
Apart from that its is just simple styling and ajax requests.
Sample:
var openingHours = getOpeningHoursFromDB();
var dayTime = "morning";
function getGreetingTime (m) {
var g = null; //return g
if(!m || !m.isValid()) { return; } //if we can't find a valid or filled moment, we return.
var split_afternoon = 12 //24hr time to split the afternoon
var split_evening = 17 //24hr time to split the evening
var currentHour = parseFloat(m);
if(currentHour >= split_afternoon && currentHour <= split_evening) {
g = "afternoon";
} else if(currentHour >= split_evening) {
g = "evening";
} else {
g = "morning";
}
return g;
}
function buildTimepicker() {
for(var openingHour in openingHours) {
if(getGreetingTime(openingHour) != dayTime) {
dayTime == getGreetingTime(openingHour);
.....
.....
}
}
}

How to set text for password field via uiautomator

I can't set text for password field via uiautomator:
UiObject eaPassword = uiDevice.findObject(new UiSelector().textContains("Password"));
assertTrue(eaPassword.waitForExists(35_000));
eaPassword.click(); // optional
eaPassword.setText("1234"); // return true
It is able to find the object itself.
If I execute eaPassword.getText(); it returns "Password".
If you run eaPassword.click() , screen would be change(keyboard pop up),
so you cannot find eaPassword(Uiobject) again
setText() this function has two behavior
1. Long press EditView
2. Input text
UiObject eaPassword = uiDevice.findObject(new UiSelector().textContains("Password"));
assertTrue(eaPassword.waitForExists(35_000));
eaPassword.setText("1234");
or try other method
UiObject eaPassword = uiDevice.findObject(new UiSelector().textContains("Password"));
assertTrue(eaPassword.waitForExists(35_000));
eaPassword.click();
setStrings("test");
public void setStrings(String text) {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 48 && c <= 57) // 0~9
uiDevice.pressKeyCode(c - 41);
else if (c >= 65 && c <= 90) // A~Z
uiDevice.pressKeyCode(c - 36, 1);
else if (c >= 97 && c < 122) // a~z
uiDevice.pressKeyCode(c - 68);
else if (c == 42) // *
uiDevice.pressKeyCode(KEYCODE_STAR);
else if (c == 35) // #
uiDevice.pressKeyCode(KEYCODE_POUND);
else if (c == 46) // .
uiDevice.pressKeyCode(KEYCODE_PERIOD);
else if (c == 47) // /
uiDevice.pressKeyCode(KEYCODE_SLASH);
else if (c == 58) // :
uiDevice.pressKeyCode(KEYCODE_SEMICOLON);
}
}
It would be one by one to input word (not use setText())

Array Integer Logic

I have a game with progress chart and an array. I want to have a chart which the player can see its score in its last 5 games.
here's my code in my array
int[] Addition = { score1, score2, score3, score4, score5 };
if (score1 == 0) {
score1 = Game.score;
} else if (score1 != 0 && score2 == 0) {
score2 = 21;
} else if (score2 != 0 && score3 == 0) {
score3 = Game.score;
} else if (score3 != 0 && score4 == 0) {
score4 = Game.score;
} else if (score4 != 0 && score5 == 0) {
score5 = Game.score;
}
What is the problem on my logic? when it runs my first game score seems to be right. but when i play one more its just that the 1st element of the array is changing? where Am I wrong? btw please apologize my english. and I appreciate any suggestions and comments. thanks guys
:::UPDATE:::
here's my code now. Can someone check if my initialization is correct:
public class ProgressGraph extends Activity {
int[] Addition = { 0, 0, 0, 0, 0 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openChart();
}
public void openChart() {
for (int i = 0; i < 5; i++) {
if (Addition[i] == 0) {
Addition[i] = Game.score;
break;
}
}
I would do something like this:
for(int i = Addition.length-1; i > 0; i--){
Addition[i] = Addition[i-1];
}
Addition[0] = Game.score;
This will mean that the most recent game will always be in position 0. If the user plays more than 5 games the oldest score gets replaced.
It also allows the user to be able to score 0.
This part of code seems to be good.
I think your score array is reset when you start the second game.
Did you try to print the scores array before the end of the second game ? Does the first score remain stored ?
Then I suggest you to use a loop like that (not tested):
for (int i = 0; i < 5; i++) {
if (score[i] == 0) {
score[i] = Game.score;
break;
}
}
Are you trying to move each old score down the list?
for (int i = 4; i > 0; i--) {
Addition[i] = Addition[i-1];
}
Addition[0] = Game.score;
In these code samples we've provided, the array values should be initialized to zero:
int[] Addition = { 0, 0, 0, 0, 0};
Taking into account your update.
- When you declare the array. The variable has a capital letter so it may have a conflict with a class. Replace Addition by addition.
With this code, if you start another activity, scores will be reset. You have to use Application extended class or SharedPreferences to save scores.

setting position of x-axis for gui label.......In Unity3D

i am making a game in which i am using gui label i want to set position of x axis like that when first character appears 2nd must appear a little further i am using.
var isClicked : boolean=false;
var xpos: float = 200;
var xpox:float;
var i:float;
function Start () {}
function xchange(): float{
xpos= xpos+8;
i=xpos;
return i;
}
function OnMouseDown()
{
isClicked = true;
}
function OnGUI()
{
if (gameObject.name == "Sphere 1(Clone)" && isClicked ){
xpox=xchange();
GUI.Label(new Rect(xpox,260,400,100), "B");
}
else if (gameObject.name == "Sphere(Clone)" && isClicked ){
xpox=xchange();
GUI.Label(new Rect(xpox,260,400,100), "A");
}
}
in this code every time i click sphere the alphabet appears but the problem is every time the alphabet appear it start on the screen it is due to this portion of code.
function xchange(): float
{
xpos= xpos+8;
i=xpos;
return i;
}
I just want that every alphabet that i click must appear a little further from first one.
you can try this :
in function Update() just check condition isClicked = true or not..
function Update()
{
if(isClicked)
{
xpos += 500;
}
}
function OnGUI()
{
if (gameObject.name == "Sphere 1(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "B");
}
}
you can try this one.. all the best..

Categories

Resources