0

I need to operate 2 different sensors (Current and Voltage) simultaneously. I have managed to run each one of them separately and I don't know how to manipulate the code so they could work together. My background in coding is basic and I'm not the one who wrote these codes.

Here are the sketches:

1. Current Sensor Code

void setup() {

  Serial.begin(9600);
}

void loop() {

  float average = 0;
  for(int i = 0; i < 1000; i++) {
    average = average + (.0264 * analogRead(A0) -13.385) / 1000;//this is 
    //for the 5A mode, if 20A or 30A mode, need to modify this formula to 
    //(.19 * analogRead(A0) -25) for 20A mode and 
    //(.044 * analogRead(A0) -3.78) for 30A mode
    delay(3);
  }
  Serial.println(average);  
}

2. Voltage Sensor Code

/*
DC Voltmeter Using a Voltage Divider
Based on Code Created By
T.K.Hareendran
*/

int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //  
float R2 = 7500.0; // 
int value = 0;
void setup(){
   pinMode(analogInput, INPUT);
   Serial.begin(9600);
   Serial.print("DC VOLTMETER");
}
void loop(){
   // read the value at analog input
   value = analogRead(analogInput);
   vout = (value * 5.0) / 1024.0; // see text
   vin = vout / (R2/(R1+R2)); 

Serial.print("INPUT V= ");
Serial.println(vin,2);
delay(500);
}
2
  • Please format your code appropriately, your question is very hard to read. Feb 7, 2018 at 18:05
  • 1
    what have you tried to combine the codes? ... you did not say anything about what you expect the output to be.
    – jsotola
    Feb 7, 2018 at 18:50

3 Answers 3

2

How to combine two sketches.

First, make sure every global variable and function name is unique (except setup and loop). Choose the smallest sketch and rename its. variables, function names and any other named items (like DEFINE).

Is convenient to refactor things. I extract the code for reading Voltage and Current to separate functions, to reduce the count of global variable and length of each function, making things easier by reducing the side of setup() and loop().

Now, combine the code inside the setup and loop:

const int analogInput = A1;

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

void loop(){
  Serial.print(getCurrent());  
  Serial.print(" "); 
  Serial.println(getVoltage(),2);
  delay(500);
}

float getCurrent() {    
  float average = 0;
  for(int i = 0; i < 1000; i++) {
    average = average + (.0264 * analogRead(A0) -13.385) / 1000;//this is 
    //for the 5A mode, if 20A or 30A mode, need to modify this formula to 
    //(.19 * analogRead(A0) -25) for 20A mode and 
    //(.044 * analogRead(A0) -3.78) for 30A mode
    delay(3);
  }
return average;
}

float getVoltage() {
   const float R1 = 30000.0; //  
   const float R2 = 7500.0; // 
   float value = analogRead(analogInput);
   float vout = (value * 5.0) / 1024.0; // see text
   float vin = vout / (R2/(R1+R2)); 
  return vin;
}
1

The bigger problem regarding combining the code is timing. You can't use delay() because the other code can't run.

You need to do let loop() run through as fast as possible and only do things when you need to i.e. when certain time elapses. And you do that with millis(), like in the Blink Without Delay example.

In your case, current reading code also needs to keep track of number of samples read and react when the sample count is reached.

const byte current_pin = A0;
const byte voltage_pin = A1;

const int max_current_sample_count = 1000;
const int current_sample_interval = 3;
const int voltage_sample_interval = 500;

// voltage divider parameters
const float R1 = 30000;
const float R2 = 7500;

// 5A current sensor parameters
const float current_sensor_scale = 0.0264;
const float current_sensor_offset = -13.385;

float current_sum = 0; // for accumulated measurements
int current_count = 0; // counter for measurements

unsigned long last_current_measure = 0; // for timing the current measurement
unsigned long last_voltage_measure = 0; // for timing the voltagemeasurement

void setup() {
    Serial.begin(9600);
}

void loop() {
    unsigned long time = millis();

    if (time - last_current_measure > current_sample_interval) { // if enough time elapsed to make another measurement
        last_current_measure = time; // save the time this action ran

        current_sum += current_sensor_scale * analogRead(current_pin) + current_sensor_offset;
        current_count++; // increase the counter
        if (current_count >= max_current_sample_count) { // when we reached number of samples
            // output the average and reset the sum and counter
            Serial.print("A = ");
            Serial.println(current_sum/current_count);
            current_sum = 0;
            current_count = 0;
        }
    }
    if (time - last_voltage_measure > voltage_sample_interval) { // if enough time elapsed to make another measurement
        last_voltage_measure = time; // save the time this action ran

        int value = analogRead(voltage_pin);
        float vout = (value * 5) / 1024.0;
        float vin = vout / (R2/(R1+R2));
        Serial.print("V = ");
        Serial.println(vin, 2);
    }
}

It is also a good practice that some values, that might need to be changed, are defined as constants. So you don't need to search for them inside the code which can become a problem, when you write more code. And it can prevent bugs, if you have those values in multiple places and forgot to change one.

0

WARNING The following advice does not generally produce compiling or working code, but is one way to start merging two separate sketches.

Without getting into the details of the code (we are not here to write code for free) I can give you the general guidelines:

  • put all the declarations (anything not contained in the setup or loop functions) at the top, first all from one sketch, then all from the other
  • copy the setup function from the first sketch and paste it after all the declarations
  • copy the content (anything inside the curly braces) of the setup function from the second sketch and put it inside the same function, but after all the first sketch code
  • rinse and repeat the last two steps for the loop functions

At this point the you might have variable name collisions and plenty of other issues, but looking at the type of code you are trying to mesh, it should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.