# Zmpt101b node for XOD

Hello! Please help me with a node for zmpt101b , I have a working sketch, but all attempts to make a node out of it myself ended in failure.
Sketch:
double sensorValue1 = 0;
double sensorValue2 = 0;
int crosscount = 0;
int climb_flag = 0;
int val[100];
int max_v = 0;
double VmaxD = 0;
double VeffD = 0;
double Veff = 0;

void setup() {
Serial.begin(9600);
pinMode(A1, INPUT);
}

void loop() {

for (int i=0; i<100; i++) {
sensorValue1 = analogRead(A1);
if (analogRead(A1) > 511) { val[i] = sensorValue1; }
else { val[i] = 0; }
delay(1);
}

max_v = 0;

for ( int i = 0; i < 100; i++ ) {
if ( val[i] > max_v ) { max_v = val[i]; }
val[i] = 0;
}
if (max_v != 0) {
VmaxD = max_v;
VeffD = VmaxD / sqrt(2);
Veff = (((VeffD - 420.76) / -90.24) * -210.2) + 210.2;
}
else { Veff = 0; }
Serial.print("Voltage: ");
Serial.println(Veff);
VmaxD = 0;

delay(100);
}

1. To simplify the code, you never use val array except to find the largest value. There is no reason to use the array and 2 loops, just look for largest value in 1st loop & save memory/time.

2. While you could implement all this in a single node, XOD is meant to be used to create building blocks. You could code all this just using existing XOD nodes without any C++ code, or you could implement “find largest of 100 reads” as a single node in C++, then use its output with built-in nodes to calculate final value & output that value. All of this could be in your main code, or included as a separate custom patch.

3. If you want to code this in C++, the simplest way to get started is look at existing nodes that do parts of this. analog-read will show you how to read the analog pin. The code for looping would be same as you have above. I would recommend using existing UART nodes for writing the output, but you could copy the code to include in your single custom node to do it all. The down side of copying code is it doesn’t get updated when XOD gets improvements unless you manually update your code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.