B4R Question Who helps me pass this code to B4R?

Discussion in 'B4R Questions' started by Johan Hormaza, May 23, 2018.

  1. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Who helps me pass this code to B4R?
    Code:
    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);
    }
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    What do you need help with?
     
  3. Cableguy

    Cableguy Expert Licensed User

    This seems like a simple analog read over a 500ms period and math operations... shouldn't be that difficult...
     
    Last edited: May 23, 2018
    BillMeyer and tigrot like this.
  4. tigrot

    tigrot Well-Known Member Licensed User

    It's a ten bit adc conversion. Each statement has a currespondent B4A one.
     
  5. tigrot

    tigrot Well-Known Member Licensed User

    Code:
    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
     

    Attached Files:

    Johan Hormaza, Erel and Cableguy like this.
  6. tigrot

    tigrot Well-Known Member Licensed User

    The code is not optimized...
     
    Johan Hormaza and Cableguy like this.
  7. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Thank you all, I thought it would be more difficult ...
     
    tigrot likes this.
  8. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    thanks, great job:):):):):):)
     
  9. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    What do you mean, it's not optimized?
     
  10. Cableguy

    Cableguy Expert Licensed 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.
     
    tigrot likes this.
  11. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Perfect
     
  12. tigrot

    tigrot Well-Known Member Licensed User

    You are right!
     
  13. tigrot

    tigrot Well-Known Member Licensed User

    Believe me. I have written in more than 100 languages and i can say they look all the same. The only issue I confuse syntax sometimes!
     
    Johan Hormaza likes this.
  14. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    And how could he do it?
     
  15. tigrot

    tigrot Well-Known Member Licensed User

    Never mind. Try this. If this works you can extend your experience.
     
    Johan Hormaza and Cableguy like this.
  16. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    If it works, all I did was add a timer and that's it!
    Thank you my brother!!!:)
     
    tigrot likes this.
  17. tigrot

    tigrot Well-Known Member Licensed User

    You are welcome. De nada. Di nulla...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice