B4R Question use inline C

davelew1s

Active Member
Licensed User
Longtime User
Hi!
I am using this code in the arduino ide with a ESP32 and it works .
'==============================
const int Analog_channel_pin= 15;
float ADC_VALUE = 0;
float voltage_value = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
ADC_VALUE = analogRead(Analog_channel_pin);
Serial.print(" ADC VALUE = ");
Serial.println(ADC_VALUE);
delay(1000);
voltage_value = (ADC_VALUE * 3.3 ) / (4095);
//voltage_value = (ADC_VALUE / 205 );
Serial.print("Voltage = ");
Serial.print(voltage_value);
Serial.print("volts");
delay(1000);
}
'======================
Maybe a dumb question, but can it be used as inlne C in B4r.
I have searched and not found anything.
Can anyone tell me if it's possible? and point me in the right direction.
Thanks Dave.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes, but:
1. This code is quite bad. You shouldn't use delay to control the program flow. You should instead use a Timer or CallSubPlus.
Timers, Loopers and CallSubPlus

2. It is simple to fix it and convert it to B4R:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private ADC As Pin
    Private ADC_VALUE As Float
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ADC.Initialize(15, ADC.MODE_INPUT)
    ReadValue(0)
End Sub

Sub ReadValue(Tag As Byte)
    ADC_VALUE = ADC.AnalogRead
    Log("ADC VALUE = ", ADC_VALUE)
    CallSubPlus("PrintValue", 1000, 0)
End Sub

Sub PrintValue(Tag As Byte)
    Dim VoltageValue As Float = ADC_VALUE * 3.3 / 4095
    Log("Voltage = ", VoltageValue, " volts")
    CallSubPlus("ReadValue", 1000, 0)
End Sub
 
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
Yes, but:
1. This code is quite bad. You shouldn't use delay to control the program flow. You should instead use a Timer or CallSubPlus.
Timers, Loopers and CallSubPlus

2. It is simple to fix it and convert it to B4R:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private ADC As Pin
    Private ADC_VALUE As Float
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ADC.Initialize(15, ADC.MODE_INPUT)
    ReadValue(0)
End Sub

Sub ReadValue(Tag As Byte)
    ADC_VALUE = ADC.AnalogRead
    Log("ADC VALUE = ", ADC_VALUE)
    CallSubPlus("PrintValue", 1000, 0)
End Sub

Sub PrintValue(Tag As Byte)
    Dim VoltageValue As Float = ADC_VALUE * 3.3 / 4095
    Log("Voltage = ", VoltageValue, " volts")
    CallSubPlus("ReadValue", 1000, 0)
End Sub

Simple when you know how!
Thanks Erel it works perfect
 
Upvote 0
Top