Welcome Guest! To enable all features please Login or Register.
Options
Go to last post Go to first unread
scott@scottelford.biz  
#1 Posted : Saturday, January 19, 2019 8:28:50 PM(UTC)
scott@scottelford.biz

Rank: Advanced Member

Groups: Registered
Joined: 5/31/2017(UTC)
Posts: 66
Australia

Thanks: 1 times
Hi Ezra,

Can you please tell me were I went wrong with this custom node. It is a copy of a working Arduino code. this a guide for me as top how to build custom nodes.

This is a basic voltage divider for 30 VDC down to 5V for input into a mega.

Once I get this sorted I will be building other custom nodes for SOC of a battery bank and there will be others.

This is a small part of my project which you have a copy of an early ver.

I have just noticed that the output activator is set for + - values and should be + for the voltage divider, it will show both on the current sensor one. Sorry.

Any help and direction you can give me will help.

Regards

Scott

<EmbrioCopy>
<Version>2.2.3.4</Version>
<Name></Name>
<UniqueID>00000000-0000-0000-0000-000000000000</UniqueID>
<Description></Description>
<NodeColor>128,128,128,255</NodeColor>
<IsBuiltIn>False</IsBuiltIn>
<ImageByteArray></ImageByteArray>
<Nodes>
<Node>
<UniqueID>3dd49fd8-f7cb-4de7-a011-1ed9ec1970e3</UniqueID>
<NodeType>Dynamic</NodeType>
<NodeName>Voltage divider</NodeName>
<UserDefinedName></UserDefinedName>
<Description></Description>
<NodeColor>229,229,197,255</NodeColor>
<X>0</X>
<Y>0</Y>
<Width>260</Width>
<DefinitionID>37f8260c-ee9a-421f-997c-be67b51e24bd</DefinitionID>
<EditingProperties>
<IsInEditMode>False</IsInEditMode>
<CodeHeight>300</CodeHeight>
<EditedLanguage>Arduino</EditedLanguage>
<IsEditingIODefinitions>False</IsEditingIODefinitions>
<UpdateCodeDefinitionID>516bf9bf-c333-4be8-920f-2535fe0ed08f</UpdateCodeDefinitionID>
</EditingProperties>

<SetLength>0</SetLength>
<SetInputs></SetInputs>
<SetOutputs></SetOutputs>
<Inputs>
<Input>
<UniqueID>34074b0e-d60a-4d33-b1c2-241b3d3ff2ff</UniqueID>
<DefinitionID>f3d9b0eb-a6b9-4b2e-b720-e1316543a416</DefinitionID>
<DefaultName>Pin number</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Integer</TypeName>
<IsOutsideConnection>False</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.IntegerNodeInput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<ConnectedOutputs></ConnectedOutputs>
<DisconnectedValue>0</DisconnectedValue>
<MinValue>0</MinValue>
<MaxValue>20</MaxValue>
</Input>

</Inputs>

<Outputs>
<Output>
<UniqueID>854fedb3-4d0d-4de0-aa69-07d8e39e3763</UniqueID>
<DefinitionID>042fcfd1-c676-447a-b2cc-c55323b0a5dc</DefinitionID>
<DefaultName>Activation</DefaultName>
<CustomName></CustomName>
<IsDisplayed>True</IsDisplayed>
<IsNative>False</IsNative>
<TypeName>Numeric</TypeName>
<IsOutsideConnection>False</IsOutsideConnection>
<IOType>Embrio.NodeEngine.NodeIO.NumericNodeOutput</IOType>
<ShowGraph>False</ShowGraph>
<LinkedIOID>00000000-0000-0000-0000-000000000000</LinkedIOID>
<MinValue>-1</MinValue>
<MaxValue>1</MaxValue>
<UseNegativeRange>True</UseNegativeRange>
<CanUseNegativeRange>True</CanUseNegativeRange>
</Output>

</Outputs>

</Node>

</Nodes>

<NodeDefinitions>
<Definition>
<Name>Voltage divider</Name>
<SoftwareVersion>2.2.3.4</SoftwareVersion>
<UniqueID>37f8260c-ee9a-421f-997c-be67b51e24bd</UniqueID>
<Description></Description>
<UseIOSet>False</UseIOSet>
<InputDefinitions>
<InputDefinition>
<Type>Embrio.NodeEngine.Definitions.IntegerNodeInputDefinition</Type>
<Name>Pin number</Name>
<UniqueID>f3d9b0eb-a6b9-4b2e-b720-e1316543a416</UniqueID>
<IsOutsideConnection>False</IsOutsideConnection>
<IsInIOSet>False</IsInIOSet>
<IsUserAdded>True</IsUserAdded>
<ConfigurationOnly>False</ConfigurationOnly>
<DisconnectedValue>0</DisconnectedValue>
<MinValue>0</MinValue>
<MaxValue>100</MaxValue>
</InputDefinition>

</InputDefinitions>

<OutputDefinitions>
<OutputDefinition>
<Type>Embrio.NodeEngine.Definitions.NumericNodeOutputDefinition</Type>
<Name>Activation</Name>
<UniqueID>042fcfd1-c676-447a-b2cc-c55323b0a5dc</UniqueID>
<IsOutsideConnection>False</IsOutsideConnection>
<IsInIOSet>False</IsInIOSet>
<IsUserAdded>True</IsUserAdded>
<NumericUseNegativeRange>False</NumericUseNegativeRange>
</OutputDefinition>

</OutputDefinitions>

<UpdateCodes>
<UpdateCode>
<UpdateCondition>Declaration</UpdateCondition>
<CodeImplementations>
<Implementation>
<Code>const int voltPin = 0;
const int numReadings = 10;

float denominator;
int resistor1 = 25000; // 32200
int resistor2 = 5000; //7520
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int inputPin = A0;
int voltage = 0;</Code>
<Language>Arduino</Language>
</Implementation>

<Implementation>
<Code></Code>
<Language>CSharp</Language>
</Implementation>

</CodeImplementations>

<UniqueID>a933fbb2-d928-4448-aca4-de34e88f05ff</UniqueID>
</UpdateCode>

<UpdateCode>
<UpdateCondition>Startup</UpdateCondition>
<CodeImplementations>
<Implementation>
<Code></Code>
<Language>CSharp</Language>
</Implementation>

<Implementation>
<Code></Code>
<Language>Arduino</Language>
</Implementation>

</CodeImplementations>

<UniqueID>8426938d-6875-4faf-b626-b0dc562871eb</UniqueID>
</UpdateCode>

<UpdateCode>
<UpdateCondition>EveryUpdate</UpdateCondition>
<CodeImplementations>
<Implementation>
<Code></Code>
<Language>CSharp</Language>
</Implementation>

<Implementation>
<Code> voltage = analogRead(voltPin);
voltage = (voltage / 1024) * 5.0; //Convert to actual voltage (0 - 5 Vdc)
voltage = voltage / denominator;

Output_Activation = transform(voltage, 0, 1024, -1, 1);</Code>
<Language>Arduino</Language>
</Implementation>

</CodeImplementations>

<UniqueID>516bf9bf-c333-4be8-920f-2535fe0ed08f</UniqueID>
</UpdateCode>

<UpdateCode>
<UpdateCondition>Shutdown</UpdateCondition>
<CodeImplementations>
<Implementation>
<Code></Code>
<Language>Arduino</Language>
</Implementation>

<Implementation>
<Code></Code>
<Language>CSharp</Language>
</Implementation>

</CodeImplementations>

<UniqueID>b56b07c0-9661-47e8-ae09-d646cd43485f</UniqueID>
</UpdateCode>

<UpdateCode>
<UpdateCondition>InputChange</UpdateCondition>
<CodeImplementations>
<Implementation>
<Code>denominator = (float)resistor2 / (resistor1 + resistor2);
for (int thisReading = 0; thisReading &amp;lt; numReadings; thisReading++) // initialize all the readings to 0:
readings[thisReading] = 0;
</Code>
<Language>Arduino</Language>
</Implementation>

</CodeImplementations>

<UniqueID>7be23c37-c2cc-4042-b59d-3f5ca55cb03a</UniqueID>
<Inputs>
<Input>f3d9b0eb-a6b9-4b2e-b720-e1316543a416</Input>
</Inputs>

</UpdateCode>

</UpdateCodes>

<NodeColor>229,229,197,255</NodeColor>
<IsBuiltIn>False</IsBuiltIn>
<TouchesPin>True</TouchesPin>
<IsLanguageLocked>False</IsLanguageLocked>
<UseCanvas>False</UseCanvas>
<CanvasHeight>150</CanvasHeight>
<CategoryName>FlowControl</CategoryName>
</Definition>

</NodeDefinitions>

</EmbrioCopy>
EmbrioAdmin  
#2 Posted : Sunday, January 20, 2019 6:39:54 PM(UTC)
EmbrioAdmin

Rank: Administration

Groups: Administrators
Joined: 12/11/2014(UTC)
Posts: 636

Thanks: 1 times
Was thanked: 36 time(s) in 33 post(s)
I'll go through the node and write any notes that come to mind.

First in the declarations section, I would remove the voltPin variable and instead in your Every Update section do analogRead(Input_PinNumber). Any variable that could be configurable should come from a node input. Once you have a node definition written you wouldn't want to have to change the code if your hardware layout changes, or if you need multiple instances of the node.

I'm not really sure how this node is supposed to work, but I think something isn't right. The OnChange event will only fire when the input "Pin number" changes, which will probably only be when the program starts. In this case it sets the denominator value, then initializes the array, but I don't see anywhere else where that array is used, so I imagine something isn't quite right.

Where do the resistor1 and 2 values come from? You might want those to be replaced by number inputs as well so you can change them without changing the node definition.

In addition to the readings array, the index and total variables are never used.

The inputPin variable is never used. I would replace that one with a number input too if you do ever use it.

Lastly, in your every update event I would check to see if the denominator is greater than 0 before trying to divide with it. I'm not sure if the OnChange or EveryUpdate event will fire first, but if the EveryUpdate fires first the denominator I think will be 0 and might break the program.

Are you implementing this based on some code somewhere? If you are stuck maybe send me that code and I can help more.



scott@scottelford.biz  
#3 Posted : Monday, January 21, 2019 7:38:28 PM(UTC)
scott@scottelford.biz

Rank: Advanced Member

Groups: Registered
Joined: 5/31/2017(UTC)
Posts: 66
Australia

Thanks: 1 times
This is the copy of the above code, I'm using this currently but it will change. This will give me a better understanding of how to write custom code that is relating to what I'm trying to do. This is older code I was playing with, it may have some blot code in it, it has been a few year since I worked on it.

The only lose guide I had was the one using the 2 axis accelerometer. So I did make a few errors..

Once I get this sorted I should be able to build the rest of the custom nodes that I require alone these lines..

All this is doing is reading battery voltage and stepping it down( via a voltage divider ) so it can be sent to the arduino, then the math rebuilds it back to what the true value is.

Resistor 1 & 2 are fixed items that make up the voltage divider, these values only change when the input voltage that is being measured is changed. eg: 30 V R1 25K & R2 5K, 12 V R1 7K & R2 5K as a guide.

So the denomiator value is fixed.

This is were I would use the serial print to show what is going on with each section of the code.

It would be handy to be able to change the values of R1 & R2 from out side of the node, as I have 3 of them in use. Above you mentioned number inputs but i don't see anything about them, I may have missed them..

//Analog volt read pin MAX 30 Vdc only,15:03 15 Jan 2019
const int voltPin = 0;
const int numReadings = 10;

float denominator;
int resistor1 = 25000; // Is fitted to an external board
int resistor2 = 5000; // as above
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
//int average = 0; // the average
int inputPin = A0;

void setup() {

Serial.begin(9600);

denominator = (float)resistor2 / (resistor1 + resistor2);
for (int thisReading = 0; thisReading < numReadings; thisReading++) // initialize all the readings to 0:
readings[thisReading] = 0;
}

void loop() {

float voltage;
// Obtain RAW voltage data
voltage = analogRead(voltPin);

voltage = (voltage / 1024) * 5.0; //Convert to actual voltage (0 - 5 Vdc)
voltage = voltage / denominator;

Serial.print("Volts: "); //Output to serial
Serial.println(voltage, 1);

delay(1000);
/*
total= total - readings[index]; // subtract the last reading:
readings[index] = analogRead(inputPin); // read from the sensor:
total= total + readings[index]; // add the reading to the total:
index = index + 1; // advance to the next position in the array:
if (index >= numReadings) // if we're at the end of the array..
index = 0; // ...wrap around to the beginning:
average = total / numReadings; // calculate the average:
Serial.print(" Avg: ");
Serial.println(average); // send it to the computer as ASCII digits
delay(10);
*/
}

Edited by user Monday, January 21, 2019 9:47:24 PM(UTC)  | Reason: Not specified

scott@scottelford.biz  
#4 Posted : Monday, January 21, 2019 10:12:23 PM(UTC)
scott@scottelford.biz

Rank: Advanced Member

Groups: Registered
Joined: 5/31/2017(UTC)
Posts: 66
Australia

Thanks: 1 times
Ezra, this might help you understand what I'm doing, this is the above Ardunio code but with more of the info left in, this is how I build up my code by adding in extra code with the serial print to see how it is going as I keep moving forward with adding in math etc. this is from 2016.

Once I'm happy with what the math is doing I remove the serial print etc as it is no longer required. It helps me to develop what I'm building, to see the data changing in real time and I can see any problems in each section.

To test this I use a 10K resistor on the input.

This is the voltage dividers idea I'm using.

//Analog volt read pin

const int voltPin = 0;

//Variables for voltage divider

float denominator;

int resistor1 = 32200;
int resistor2 = 7500;
int voltage = A0;

void setup() {

Serial.begin(9600);
//Convert resistor values to division value
// Equation is previously mentions voltage divider equation
// R2 / (R1 + R2)
// In this case returns 0.20 or 1/5
denominator = (float)resistor2 / (resistor1 + resistor2);
} // Void Setup Close

void loop() {

float voltage;
//Obtain RAW voltage data
voltage = analogRead(voltPin);
//Convert to actual voltage (0 - 5 Vdc)
voltage = (voltage / 1024) * 5.0;
//Convert to voltage before divider
// Divide by divider = multiply
// Divide by 1/5 = multiply by 5
voltage = voltage / denominator;
//Output to serial
Serial.print("Raw: ");
Serial.print(voltage);
Serial.print("Volts: ");
Serial.println(voltage);
//Delay to make serial out readable
delay(500);
} // void loop close
scott@scottelford.biz  
#5 Posted : Monday, January 21, 2019 10:26:04 PM(UTC)
scott@scottelford.biz

Rank: Advanced Member

Groups: Registered
Joined: 5/31/2017(UTC)
Posts: 66
Australia

Thanks: 1 times
This next code is for a power meter, still to be fine tuned but this will also be used in some for in my project.

Again you can see how I use the serial monitor to check how the code & math are going.

This one I believe will have to be made up of several custom nodes. This is to help you understand where I'm going and what I'm trying to build.

Please ask questions if you don't understand something, i'm still learning about this also as i go. It's fun most of the time. BigGrin

As you can see once I get a handle on the voltage divider and were & what parts of the code to use, i will progress to the next part and then onto the following custom node build.

I code most of this in Note ++, before I load it into the Arduino just for info..

// Watt meter 24 Volt BI adding ampHR

int batMonPin = A0; // input pin for the voltage divider
int batVal = 0; // variable for the A/D value
float pinVoltage = 0; // variable to hold the calculated voltage
float batteryVoltage = 0;

int analogInputPin = A1; // Analog input pin current output in mA
int sensorValue = 0;
int outputValue = 0;
int currentSensor = 0;
unsigned long msec = 0;
float time = 0.0;
int sample = 0;
float totalCharge = 0.0;
float averageAmps = 0.0;
float ampSeconds = 0.0;
float ampHours = 0.0;
float wattHours = 0.0;
float amps = 0.0;
float batteryState = 0;

int R1 = 29000; // Resistance of R1 in ohms
int R2 = 5000; // Resistance of R2 in ohms

float ratio = 0; // Calculated from R1 / R2

void setup() {

Serial.begin(9600); // initialize serial communications at 9600 bps:

}

void loop() {

int sampleBVal = 0;
int avgBVal = 0;
int sampleAmpVal = 0;
int avgSAV = 0;

// ---------------------------------------------------------------------------------

// Volts, Amps & Power Value readings & calculations

for (int x = 0; x < 10; x++){ // run through loop 10x
{
double Current = (AnalogInputPin); // Read analog value
Serial.print(", V From Null: ");
printDouble(Current, 2); // display Current
Serial.print(" A");
Serial.println("");
delay(3000);

// --------------------------------------------------------------------------------------------------------


--------------------------- Current sensing not working BI ---------------
sensorValue = analogRead(analogInPin); // read A1 the analog in value from current sensor
sampleAmpVal = sampleAmpVal + sensorValue; // add samples together

avgSAV = sampleAmpVal / 10;
// Serial.print("Raw current= ");
// Serial.print(sampleAmpVal);


// Serial.print(" Raw volt= ");
// Serial.println(sampleBVal);

delay (1000); // let ADC settle before next sample

}

outputValue = ((sensorValue * 4.77) / 1024) / 0.06; // convert to milli amps - 0.5724


// Print decimal numbers
/*
void printDouble(double val, byte precision) {

Serial.print (int(val)); // Print int part
if( precision > 0) { // Print decimal part
Serial.print(".");
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) Serial.print("0");
Serial.print(frac,DEC);
}
}

// Read 1.1V reference against AVcc

long readInternalVcc() {

long result;
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}

// Calculate current with Allegro ACS714

double currentSensor(int RawADC) {

int Sensitivity = 66; // mV/A // orginal value is 66, for a 30 amp sensor
long InternalVcc = readInternalVcc();
double ZeroCurrentVcc = InternalVcc / 2;
double SensedVoltage = (RawADC * InternalVcc) / 1024;
double Difference = SensedVoltage - ZeroCurrentVcc;
double SensedCurrent = Difference / Sensitivity;
Serial.print("ADC: ");
Serial.print(RawADC);
Serial.print("/1024");
Serial.print(", Sensed Voltage: ");
printDouble(SensedVoltage, 1);
Serial.print("mV");
Serial.print(", 0A at: ");
printDouble(ZeroCurrentVcc, 1);
Serial.print("mV");
return SensedCurrent; // Return the Current
}

*/


// ----------------------- Voltage sensing --------------------------------------

batVal = analogRead(batMonPin); // read A0 the voltage on the divider
sampleBVal = sampleBVal + batVal; // add samples together

avgBVal = sampleBVal / 10; //divide by 10 (number of samples) to get a steady reading

pinVoltage = batVal * 0.00655; // Calculate the voltage on the A/D pin

ratio = (float)R1 / (float)R2; // Use the ratio calculated for the voltage divider
batteryVoltage = pinVoltage * ratio; // to calculate the battery voltage

amps = outputValue;
float watts = amps * batteryVoltage;

Serial.print("Volts = " );
Serial.print(batteryVoltage);
Serial.print("\t Current (amps) = ");
Serial.print(amps);
Serial.print("\t Power (Watts) = ");
Serial.print(watts);

// ------------------------------------------------------------------------------

// Time section

sample = sample + 1;

msec = millis();

time = (float) msec / 1000.0;

totalCharge = totalCharge + amps;

averageAmps = totalCharge / sample;

ampSeconds = averageAmps*time;

ampHours = ampSeconds/3600;

wattHours = batteryVoltage * ampHours;

Serial.print("\t Time (hours) = ");
Serial.print(time/3600);

Serial.print("\t Amp Hours (ah) = ");
Serial.print(ampHours);

Serial.print("\t Watt Hours (wh) = ");
Serial.print(wattHours);

batteryState = 300 - ampHours; // 300 Ah batt bank capacity

Serial.print("\t Batt Capacity Left (ah) = ");
Serial.println(batteryState);


// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(1000);
}
}


scott@scottelford.biz  
#6 Posted : Monday, January 21, 2019 10:27:06 PM(UTC)
scott@scottelford.biz

Rank: Advanced Member

Groups: Registered
Joined: 5/31/2017(UTC)
Posts: 66
Australia

Thanks: 1 times
Sorry for the above, i tried to copy a zipped file but it didn't want to play nicely with me.

so I will stop there and await your feedback Ezra..

Edited by user Monday, January 21, 2019 10:27:45 PM(UTC)  | Reason: Not specified

Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Notification

Icon
Error