B4R Question Who helps me pass this code to B4R?

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Who helps me pass this code to B4R?
B4X:
void setup() {
 
  Serial.begin(9600);
  analogReference(INTERNAL);
  //analogReference(INTERNAL1V1); //solo Arduino Mega
}

void loop() {
 
  float Irms=get_corriente(); //Corriente eficaz (A)
  float P=Irms*220.0; // P=IV (Watts)

  Serial.print("Irms: ");
  Serial.print(Irms,3);
  Serial.print("A, Potencia: ");
  Serial.print(P,3); 
  Serial.println("W");
  //delay(100);     
}

float get_corriente()
{
  float voltajeSensor;
  float corriente=0;
  float Sumatoria=0;
  long tiempo=millis();
  int N=0;
  while(millis()-tiempo<500)//Duración 0.5 segundos(Aprox. 30 ciclos de 60Hz)
  {
    voltajeSensor = analogRead(A0) * (1.1 / 1023.0);////voltaje del sensor
    corriente=voltajeSensor*30.0; //corriente=VoltajeSensor*(30A/1V)
    Sumatoria=Sumatoria+sq(corriente);//Sumatoria de Cuadrados
    N=N+1;
    delay(1);
  }
  Sumatoria=Sumatoria*2;//Para compensar los cuadrados de los semiciclos negativos.
  corriente=sqrt((Sumatoria)/N); //ecuación del RMS
  return(corriente);
}
 

tigrot

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public analog As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    analog.Initialize(analog.A0,analog.MODE_INPUT)
    Log("AppStart")
    Dim Irms As Float=get_corriente' //Corriente eficaz (A)
    Dim P As Float=Irms*220.0' // P=IV (Watts)
    Log("Irms: ",Irms,"A, Potencia: ",P,"W")
End Sub
Sub get_corriente As Float
    Dim voltajeSensor As Float
    Dim corriente=0 As Float
    Dim Sumatoria=0 As Float
    Dim tiempo=Millis() As Long
    Dim N=0 As Int
    Do While(Millis()-tiempo<500) ' Duración 0.5 segundos(Aprox. 30 ciclos de 60Hz)
        voltajeSensor = analog.analogRead * (1.1 / 1023.0) 'voltaje del sensor
        corriente=voltajeSensor*30.0 'corriente=voltajeSensor*(30A/1V)
        Sumatoria=Sumatoria+(corriente*corriente) 'Sumatoria de Cuadrados
        N=N+1
        Delay(1)
    Loop
    Sumatoria=Sumatoria*2' Para compensar los cuadrados de los semiciclos negativos.
    corriente=Sqrt((Sumatoria)/N)'ecuación del RMS
    Return(corriente)
End Sub
Ciao
Mauro
 

Attachments

  • testanalog.zip
    1.1 KB · Views: 208
Upvote 0

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public analog As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    analog.Initialize(analog.A0,analog.MODE_INPUT)
    Log("AppStart")
    Dim Irms As Float=get_corriente' //Corriente eficaz (A)
    Dim P As Float=Irms*220.0' // P=IV (Watts)
    Log("Irms: ",Irms,"A, Potencia: ",P,"W")
End Sub
Sub get_corriente As Float
    Dim voltajeSensor As Float
    Dim corriente=0 As Float
    Dim Sumatoria=0 As Float
    Dim tiempo=Millis() As Long
    Dim N=0 As Int
    Do While(Millis()-tiempo<500) ' Duración 0.5 segundos(Aprox. 30 ciclos de 60Hz)
        voltajeSensor = analog.analogRead * (1.1 / 1023.0) 'voltaje del sensor
        corriente=voltajeSensor*30.0 'corriente=voltajeSensor*(30A/1V)
        Sumatoria=Sumatoria+(corriente*corriente) 'Sumatoria de Cuadrados
        N=N+1
        Delay(1)
    Loop
    Sumatoria=Sumatoria*2' Para compensar los cuadrados de los semiciclos negativos.
    corriente=Sqrt((Sumatoria)/N)'ecuación del RMS
    Return(corriente)
End Sub
Ciao
Mauro
thanks, great job:):):):):):)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
it means that he simply "translated" the C code into B4R but some tweaking or even a different approach is still possible to achieve a better performance.
 
Upvote 0
Top