I have a very simple example of what i am trying to achieve. basically i want to be able to read a number entered from RemoteXy via bluetooth and than count up from that value.
/*
-- New project --
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.3.5 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.3.1 or later version;
- for iOS 1.3.5 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 2
#define REMOTEXY_SERIAL_TX 3
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,2,0,0,0,12,0,8,13,0,
7,52,36,28,20,5,2,26,2 };
// this structure defines all the variables of your control interface
struct {
// input variable
int16_t edit_1; // −32767.. +32767
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
int x = 0;
void setup()
{
RemoteXY_Init ();
Serial.begin (9600);
}
void loop()
{
RemoteXY_Handler ();
x = RemoteXY.edit_1; // read value from phone
x++; // count up when from value entered.
Serial.println (x);
}
I am able to read the value and count up by 1 but that's as far as it's goes.
i have tried a few different approaches to this but end up getting to this same issue.
void loop()
{
RemoteXY_Handler ();
x = RemoteXY.edit_1; // read value from phone
x++; // count up when from value entered.
Serial.println (x);
}
What is changing the value in the struct? If the value in RemoteXY.edit_1 is 5 then you read 5 into x, increment and print the 6. Then on the next loop you read the 5 from the struct increment and print the 6. Then on the next loop you read the 5 from the struct increment and print the 6.
If you want to keep counting then you either have to stop putting the value from the struct into x or you have to update the value in the struct.
yes i had assumed that this was refreshing the same value with every loop.
[quote
If you want to keep counting then you either have to stop putting the value from the struct into x or you have to update the value in the struct.
[/quote]
If x is always updating it's value with every loop but with the same value, Is there a way that i can copy this value to another int and then count up separately, only resetting the count up if x is changed.
Bails:
If x is always updating it's value with every loop but with the same value, Is there a way that i can copy this value to another int and then count up separately, only resetting the count up if x is changed.
You could introduce a second integer and simple save the value, I have added the if loop, check it out:
void loop()
{
RemoteXY_Handler ();
x = RemoteXY.edit_1; // read value from phone
if(x != x_old){ x_old = x;}
x++; // count up when from value entered.
Serial.println (x);
}
So i managed to get working mainly by luck, with a lot of trial and error. I don't fully understand why this works but it works.
/*
-- New project --
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.3.5 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.3.1 or later version;
- for iOS 1.3.5 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 2
#define REMOTEXY_SERIAL_TX 3
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,2,0,0,0,12,0,8,13,0,
7,52,36,28,20,5,2,26,2 };
// this structure defines all the variables of your control interface
struct {
// input variable
int16_t edit_1; // −32767.. +32767
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
int x = 0;
int counter = 0;
void setup()
{
RemoteXY_Init ();
Serial.begin (9600);
}
void loop()
{
RemoteXY_Handler ();
if (x != RemoteXY.edit_1){
x = RemoteXY.edit_1; // read value from phone
counter = x;
}
if(x== RemoteXY.edit_1){
counter++;// count up when from value entered}
}
Serial.println (counter);
}
Is there any suggestions on formatting or improvement to this piece of code? still fairly new and learning.
Okay let me explain the code for you. Here, in the first part:
RemoteXY_Handler ();
if (x != RemoteXY.edit_1){
x = RemoteXY.edit_1; // read value from phone
counter = x;
}
You are getting the value of RemoteXY.edit_1 and then checking if it's not equal to x (which must have the previous value) then save the value in x and counter.
After that in second part:
if(x== RemoteXY.edit_1){
counter++;// count up when from value entered}
}
Serial.println (counter);
Here now if the value of RemoteXY.edit_1 is not changed then you are simply incrementing the counter.