Just a little help please

Support forum for MegunoLink
Post Reply
supershepp
Posts: 10
Joined: Mon Jan 07, 2019 12:12 am

Thu Jan 10, 2019 4:53 am

Hello anyone out there?
So I need a little help. I have written this program that will require a floating value to be sent from Arduino to Meguno.

User inputs an one decimal number (23.5),into a number box. Then click a button to broadcast on the serial bus the number. Arduino receives that number (loco) and drives a motor one direct or the other depending on the rigs current location(DataValue). OK, here is where trouble comes in handy. The rig is connected to a pot(A0) that ready analog value, maps the range, and should report back to meguno its current location. An text box or number box should display an one decimal number.

Link to files https://drive.google.com/open?id=1Ya_Qp ... cztrtqEhGh

Question, how do I get my arduino to conform to those behavior?
Also, changing colour commands does not work. Any idea?


Please disregard the jibber jabber. Alot of brain cells have been fried getting this far.
#include "MegunoLink.h"
#include "CommandHandler.h"
float Loco;
float DataValue;
int BlinkDelayTime = 500;

//serial interface button
InterfacePanel MyPanel;

CommandHandler<> SerialCommandHandler; //serial comms

//void Cmd_GetLoco2(CommandParameter &Parameters)
// {
//Serial.print("GetLoco2");
//Loco=Parameters.NextParameterAsInteger();// needed for comms
// }
void Cmd_SetLoco(CommandParameter &Parameters)
{
Loco=Parameters.NextParameterAsInteger(Loco);// needed for comms
}

const int pot = A2;// open a ver box for location bar
const int Adv = 2;// open a var box for advancing the motor
const int Stay = 3;// open a var box to stop movement
const int Ret = 4;// open a var box for retracting the motor

void setup()
{
Serial.begin(9600);
pinMode(Adv,OUTPUT);
pinMode(Stay,OUTPUT);
pinMode(Ret,OUTPUT);

//SerialCommandHandler.AddCommand(F("SetLoco"), Cmd_SetLoco);// commands to read interface commands
SerialCommandHandler.AddVariable(F("SetLoco"), Loco);
}
void loop()
{

DataValue = analogRead(pot);
DataValue = map(DataValue, 0, 1023, 0, 50);
Serial.print("Loco "); Serial.println(Loco); // this will show me values. for test only
Serial.print("DataValue "); Serial.println(DataValue);// this will show me values. for test only


//Progress bars
MyPanel.SetProgress(F("IP9"), DataValue);
// Set control value
MyPanel.SetNumber(F("IPNumericUpDown1"), DataValue,2);

if (Loco == 0)
{MyPanel.SetText(F("Set"), "Stopped");}
if (Loco > 0)
{MyPanel.SetText(F("Set"), "Running");}
//MyPanel.SetForeColor(F("Status"), "Red");
// MyPanel.SetBackColor(F("Status"), "Blue");

if (Loco > DataValue)
{
digitalWrite(Adv, HIGH);
digitalWrite(Stay, LOW);
digitalWrite(Ret, LOW);
MyPanel.SetText(F("ToP9"), "To Pin");
delay (BlinkDelayTime);
MyPanel.SetText(F("ToP9"), "<--- ");
delay (BlinkDelayTime);

//MyPanel.SetForeColor(F("Status"), "Green");
}
if (Loco < DataValue)
{
digitalWrite(Adv, LOW);
digitalWrite(Stay, LOW);
digitalWrite(Ret, HIGH);
MyPanel.SetText(F("FromP9"), "From Pin");
delay (BlinkDelayTime);
MyPanel.SetText(F("FromP9"), "---> ");
delay (BlinkDelayTime);
//MyPanel.SetForeColor(F("Status"), "Blue");
}

if (Loco == DataValue)
{
digitalWrite(Adv, LOW);
digitalWrite(Stay, HIGH);
digitalWrite(Ret, LOW);

MyPanel.SetText(F("HoldP9"), "HOLD");
delay (BlinkDelayTime);
MyPanel.SetText(F("HoldP9"), "---- ");
delay (BlinkDelayTime);

//MyPanel.SetForeColor(F("Status"), "Red");
}
// command handler instrucion
SerialCommandHandler.Process();
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Thu Jan 10, 2019 6:09 am

Hi, see my other response regarding the colour. As for sending a decimal number I think you need to get the string and turn it into a float. Like this

void Cmd_SetTurtleCount(CommandParameter &Parameters)
{
float MyFloat = atof(Parameters.NextParameter());
Serial.println(MyFloat);
}

Cheers
Phil
supershepp
Posts: 10
Joined: Mon Jan 07, 2019 12:12 am

Thu Jan 10, 2019 10:37 pm

Thanks Philr for your help with this. I was able to get the color option working. I used the example in the Meguno for that line which includs the F() clause.

Now about this Decimal. i am able to get my number into the proper format for the Var called "loco". the problem is when the analogue value is read and that value is put into the var (DataValue). it will not accept a decimal.
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Fri Jan 11, 2019 4:04 am

Post your code and I'll have a look.
Phil
supershepp
Posts: 10
Joined: Mon Jan 07, 2019 12:12 am

Fri Jan 11, 2019 7:06 pm

So i have learned a few things that may help others in the future.

I stated that i wanted my float var to print a decimal. I learned that is you are making a calculation you can put the "." in your code line an it will send over the remaining numbers.

so lets say your code reads like this:

float Data = analogRead(pot);
float DataValue(Data* 50/1024);
Serial.print (DataValue);// this will output the value without decimal places.

But if you add that little "Dot"

float Data = analogRead(pot);
float DataValue(Data* 50./1024); // dot after 50
Serial.print (DataValue); // this will output the value with two decimal places.

who knew it was that simple
Post Reply