So I have been using the Arduino IDE and I am sort of a newbie when it comes to troubleshooting issues. I am creating code for my project and I ran into an issue that involved the framework of the code itself. (If this is confusing please let me elaborate). Here is the code: (ignoring the stuff before void setup)
void setup() {
pinMode(MODE_BUTTON_PIN, INPUT);
pinMode(CAPACITANCE_LED_1, OUTPUT);
pinMode(CAPACITANCE_LED_2, OUTPUT);
pinMode(INDUCTANCE_LED, OUTPUT);
pinMode(CAPACITANCE_IN, OUTPUT);
pinMode(CAPACITANCE_OUT, OUTPUT);
pinMode(CAPACITANCE_CHARGE_PIN, OUTPUT);
digitalWrite(CAPACITANCE_DISCHARGE_PIN, LOW);
Serial.begin(115200);
lcd.begin(16,2);
}
void loop() {
buttonState = digitalRead(MODE_BUTTON_PIN);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 3 == 0) { //Creating a PWM output with analog IN pins
analogWrite(CAPACITANCE_LED_1, 255);
analogWrite(CAPACITANCE_LED_2, 0);
analogWrite(INDUCTANCE_LED, 0);
delayMicroseconds(1);
analogWrite(CAPACITANCE_LED_1, 0);
delayMicroseconds(500);
Serial.print("Capacitance: ");
digitalWrite(CAPACITANCE_CHARGE_PIN, HIGH);
startTime = micros();
while(analogRead(CAPACITANCE_ANALOG) < 648){
}
elapsedTime= micros() - startTime;
microFarads = ((float)elapsedTime / resistorValue) ;
if (microFarads > 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SCALE: 0.1uF-4F");
lcd.setCursor(0,1);
lcd.print(microFarads);
lcd.setCursor(16,1);
lcd.print("uF");
delay(500);
}
else{
nanoFarads = microFarads * 1000.0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SCALE: 0.1uF-4F");
lcd.setCursor(0,1);
lcd.print(nanoFarads);
lcd.setCursor(14,1);
lcd.print("nF");
delay(500);
}
digitalWrite(CAPACITANCE_CHARGE_PIN, LOW);
pinMode(CAPACITANCE_DISCHARGE_PIN, OUTPUT);
digitalWrite(CAPACITANCE_DISCHARGE_PIN, LOW); //discharging the capacitor
while(analogRead(CAPACITANCE_ANALOG) > 0){
}//This while waits till the capaccitor is discharged
pinMode(CAPACITANCE_DISCHARGE_PIN, INPUT); //this sets the pin to high impedance
lcd.setCursor(0,0);
lcd.print("DISCHARGING.....");
lcd.setCursor(0,1);
}
if (buttonPushCounter % 3 == 1) { //Creating a PWM output with analog IN pins
analogWrite(CAPACITANCE_LED_1, 0);
analogWrite(CAPACITANCE_LED_2, 255);
analogWrite(INDUCTANCE_LED, 0);
delayMicroseconds(1);
analogWrite(CAPACITANCE_LED_2, 0);
delayMicroseconds(500);
Serial.println("Capacitance:");
}
if (buttonPushCounter % 3 == 2) { //Creating a PWM output with analog IN pins
analogWrite(CAPACITANCE_LED_1, 0);
analogWrite(CAPACITANCE_LED_2, 0);
analogWrite(INDUCTANCE_LED, 255);
delayMicroseconds(1);
analogWrite(INDUCTANCE_LED, 0);
delayMicroseconds(500);
Serial.println("Inductance");
}
}
First of all, I am running a sketch for capacitance measurement device that my Nano runs. When I upload the sketch, the first thing that happens is that my LED flashes very briefely and does'nt perform the function I want it to. And I know that this is software related. What I am asking is is it possible to have a void loop run independently of another void loop, but given the same conditions? In other words, is it possible to have 2 different loops run, but independently of one another? Sorry if this thread is confusing.