I am making a rig for cyclic testing of turning solenoids on/off via relays.
I had issues with solenoids not staying off for the amount of time specified in the code. May have been voltage spikes from solenoids. Using diodes solved the issue.
The code for turning the solenoids on and off is working properly.
The code for measuring current is working properly as well.
(Reference: acs712 current sensor 5A - Sensors - Arduino Forum)
When I combine the two codes the measurement of current is not showing up on the serial monitor. Is there something wrong with the void functions I placed in between the delay times?
CODE FOR TURNING ON/OFF 8 RELAYS/8 SOLENOIDS
#include <LiquidCrystal.h>
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // initialize library and associate LCD pins with Arduino pins
unsigned long TOn = 30000; // delay time to turn relay on/off (60,000 ms = 1 minute)
unsigned long TOff = 10000;
void setup () {
lcd.begin(16, 2); // LCD's columns and rows: 16 columns, 2 rows
lcd.setCursor(0, 0); // Printing position on lcd screen is column 0, row 0
Serial.begin(9600);
lcd.print("On:");
lcd.print(TOn/60000);
lcd.print("m");
lcd.print(",");
lcd.print("Off:");
lcd.print(TOff/60000);
lcd.print("m");
DDRA = B11111111; // set PORTA (digital 22-29) to outputs
int maxnumber = 5; // set cycle count
for (int count = 0; count < maxnumber;) //Counter will stop counting after certain amount of cycles
{
lcd.setCursor(0, 1); //clears lcd before incrementing to next number
count++;
lcd.print("Cycle Count:");
lcd.print(count);
PORTA = B00000000;
delay(TOn); //relay on time
PORTA = B11111111;
delay(TOff); //relay off time
}
DDRA = B00000000;
}
void loop(void){
}
CODE FOR POWERING SOLENOIDS ON AND OFF AND MEASURING CURRENT SIMULTANEOUSLY.
#include <LiquidCrystal.h>
int mVperAmp = 185; // 185 (5A), 100 (20A), 66 (30A) mvperAmp value used, using ACS712 sensors
const int numReadings = 64;
const int AIN1 = A1;
const int AIN2 = A2;
const int AIN3 = A3;
const int AIN4 = A4;
const int AIN5 = A5;
const int AIN6 = A6;
const int AIN7 = A7;
const int AIN8 = A8;
unsigned int RawValue1 = 0; // can hold up to 64 10-bit A/D readings
unsigned int RawValue2 = 0;
unsigned int RawValue3 = 0;
unsigned int RawValue4 = 0;
unsigned int RawValue5 = 0;
unsigned int RawValue6 = 0;
unsigned int RawValue7 = 0;
unsigned int RawValue8 = 0;
float ACSoffset1 = 2478.006;
float ACSoffset2 = 2497.556;
float ACSoffset3 = 2468.231;
float ACSoffset4 = 2473.118;
float ACSoffset5 = 2487.781;
float ACSoffset6 = 2487.781;
float ACSoffset7 = 2502.444;
float ACSoffset8 = 2487.781;
double Voltage1 = 0;
double Voltage2 = 0;
double Voltage3 = 0;
double Voltage4 = 0;
double Voltage5 = 0;
double Voltage6 = 0;
double Voltage7 = 0;
double Voltage8 = 0;
double Amps1 = 0;
double Amps2 = 0;
double Amps3 = 0;
double Amps4 = 0;
double Amps5 = 0;
double Amps6 = 0;
double Amps7 = 0;
double Amps8 = 0;
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // initialize library and associate LCD pins with Arduino pins
unsigned long TOn = 10000; // delay time to turn relay on/off (60,000 ms = 1 minute)
unsigned long TOff = 5000;
void setup () {
lcd.begin(16, 2); // LCD's columns and rows: 16 columns, 2 rows
lcd.setCursor(0, 0); // Printing position on lcd screen is column 0, row 0
Serial.begin(9600);
lcd.print("On:");
lcd.print(TOn/60000);
lcd.print("m");
lcd.print(",");
lcd.print("Off:");
lcd.print(TOff/60000);
lcd.print("m");
DDRA = B11111111; // set PORTA (digital 22-29) to outputs
int maxnumber = 5; // set cycle count
for (int count = 0; count < maxnumber;) //Counter will stop counting after certain amount of cycles
{
lcd.setCursor(0, 1); //clears lcd before incrementing to next number
count++;
lcd.print("Cycle Count:");
lcd.print(count);
PORTA = B00000000;
void ReadCurrent();
delay(TOn); //relay on time
void ReadCurrent();
PORTA = B11111111;
void ReadCurrent();
delay(TOff); //relay off time
}
DDRA = B00000000;
}
void loop(void){
}
void ReadCurrent()
{
for (int x = 0; x < numReadings; x++) // 64 analogue readings for averaging
{
RawValue1 = RawValue1 + analogRead(AIN1); // add each A/D reading to a total
RawValue2 = RawValue2 + analogRead(AIN2);
RawValue3 = RawValue3 + analogRead(AIN3);
RawValue4 = RawValue4 + analogRead(AIN4);
RawValue5 = RawValue5 + analogRead(AIN5);
RawValue6 = RawValue6 + analogRead(AIN6);
RawValue7 = RawValue7 + analogRead(AIN7);
RawValue8 = RawValue8 + analogRead(AIN8);
}
Voltage1 = ((RawValue1 / numReadings) / 1023.0) * 5000; // Gets you mV
Voltage2 = ((RawValue2 / numReadings) / 1023.0) * 5000;
Voltage3 = ((RawValue3 / numReadings) / 1023.0) * 5000;
Voltage4 = ((RawValue4 / numReadings) / 1023.0) * 5000;
Voltage5 = ((RawValue5 / numReadings) / 1023.0) * 5000;
Voltage6 = ((RawValue6 / numReadings) / 1023.0) * 5000;
Voltage7 = ((RawValue7 / numReadings) / 1023.0) * 5000;
Voltage8 = ((RawValue8 / numReadings) / 1023.0) * 5000;
Amps1 = ((Voltage1 - ACSoffset1) / mVperAmp);
Amps2 = ((Voltage2 - ACSoffset2) / mVperAmp);
Amps3 = ((Voltage3 - ACSoffset3) / mVperAmp);
Amps4 = ((Voltage4 - ACSoffset4) / mVperAmp);
Amps5 = ((Voltage5 - ACSoffset5) / mVperAmp);
Amps6 = ((Voltage6 - ACSoffset6) / mVperAmp);
Amps7 = ((Voltage7 - ACSoffset7) / mVperAmp);
Amps8 = ((Voltage8 - ACSoffset8) / mVperAmp);
Serial.print("Raw Value 1 = " ); // shows pre-scaled value
Serial.print(RawValue1 / numReadings);
Serial.print("\t mV 1 = "); // shows the voltage measured
Serial.print(Voltage1, 3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps 1 = ");
Serial.println(Amps1, 3);
Serial.print("Raw Value 2 = " );
Serial.print(RawValue2 / numReadings);
Serial.print("\t mV 2 = ");
Serial.print(Voltage2, 3);
Serial.print("\t Amps 2 = ");
Serial.println(Amps2, 3);
Serial.print("Raw Value 3 = " );
Serial.print(RawValue3 / numReadings);
Serial.print("\t mV 3 = ");
Serial.print(Voltage3, 3);
Serial.print("\t Amps 3 = ");
Serial.println(Amps3, 3);
Serial.print("Raw Value 4 = " );
Serial.print(RawValue4 / numReadings);
Serial.print("\t mV 4 = ");
Serial.print(Voltage4, 3);
Serial.print("\t Amps 4 = ");
Serial.println(Amps4, 3);
Serial.print("Raw Value 5 = " );
Serial.print(RawValue5 / numReadings);
Serial.print("\t mV 5 = ");
Serial.print(Voltage5, 3);
Serial.print("\t Amps 5 = ");
Serial.println(Amps5, 3);
Serial.print("Raw Value 6 = " );
Serial.print(RawValue6 / numReadings);
Serial.print("\t mV 6 = ");
Serial.print(Voltage6, 3);
Serial.print("\t Amps 6 = ");
Serial.println(Amps6, 3);
Serial.print("Raw Value 7 = " );
Serial.print(RawValue7 / numReadings);
Serial.print("\t mV 7 = ");
Serial.print(Voltage7, 3);
Serial.print("\t Amps 7 = ");
Serial.println(Amps7, 3);
Serial.print("Raw Value 8 = " );
Serial.print(RawValue8 / numReadings);
Serial.print("\t mV 8 = ");
Serial.print(Voltage8, 3);
Serial.print("\t Amps 8 = ");
Serial.println(Amps8, 3);
Serial.println();
RawValue1 = 0; // reset value
RawValue2 = 0;
RawValue3 = 0;
RawValue4 = 0;
RawValue5 = 0;
RawValue6 = 0;
RawValue7 = 0;
RawValue8 = 0;
delay(3000);
}