/******************************************************************** * Filename: 555Calc.ino * * This program calculates the frequency, high time, low time, and * duty cycle for an astable oscillator on a 555 timer chip when * given the capacitance and resistance values. * * It also calculates the high time for a mono-stable "One Shot". * * ********************************************************************/ int onpin = 2; int offpin = 3; int button = 4; long mult = 1000000; // Multiplier for capacitance. /******************************************************************** * astgpio() function - Run Arduino simulation of astable oscillator. ********************************************************************/ void astgpio(long ondelay, long offdelay) { long ontime; long offtime; int done = 0; if(ondelay > 30000) ondelay = 30000; // If light stays the same for over if(offdelay > 30000) offdelay = 30000; // thirty seconds it would be boring, // so change it after thirty seconds. while(digitalRead(button) == HIGH); // Wait for buttonpress while(digitalRead(button) == LOW); // Debounce delay(10); while(done == 0) { ontime = millis(); while((millis() - ontime) < ondelay) { digitalWrite(onpin, HIGH); digitalWrite(offpin, LOW); if(digitalRead(button) == LOW) // If button pressed end Arduino simulation. { while(digitalRead(button) == LOW); // Debounce done = 1; ontime = 0; delay(10); } } offtime = millis(); while(((millis() - offtime) < offdelay) && (done == 0)) { digitalWrite(offpin, HIGH); digitalWrite(onpin, LOW); if(digitalRead(button) == LOW) // If button pressed end Arduino simulation. { while(digitalRead(button) == LOW); // Debounce done = 1; ontime = 0; delay(10); } } } digitalWrite(onpin, LOW); digitalWrite(offpin, LOW); } /******************************************************************** * astable() function - The code for an astable oscillator. ********************************************************************/ void astable() { float r1 = 0; float r2 = 0; float c1 = 0; Serial.println(F("555 Timer in Astable mode")); Serial.println(' '); Serial.print(F(" Enter R1: ")); Serial.flush(); while(Serial.available() == 0); if (Serial.available()) r1 = Serial.parseFloat(); Serial.println(r1); Serial.println(' '); Serial.print(F(" Enter R2: ")); Serial.flush(); while(Serial.available() == 0); if (Serial.available()) r2 = Serial.parseFloat(); Serial.println(r2); Serial.println(' '); if(mult==1000000000) Serial.print(F("Enter C1 in nF: ")); else if(mult==1000000) Serial.print(F("Enter C1 in uF: ")); else if(mult==1) Serial.print(F(" Enter C1 in F: ")); Serial.flush(); while(Serial.available() == 0); if (Serial.available()) c1 = Serial.parseFloat(); Serial.println(c1,5); Serial.println(' '); Serial.println("----------------------------------------"); Serial.println(' '); c1/=mult; float freq = 1.44 / ((r1 + r2 + r2) * c1); float hi = 0.693 * (r1 + r2) * c1; float lo = 0.693 * r2 * c1; float duty = (hi / (hi + lo)) * 100; Serial.print(F(" Frequency = ")); Serial.println(freq,5); Serial.print(F(" Time high = ")); Serial.println(hi,5); Serial.print(F(" Time low = ")); Serial.println(lo,5); Serial.print(F(" Duty Cycle = ")); Serial.print(duty,2); Serial.println(F(" Percent")); long lhi = hi*1000; // Convert high and low times from float to long milliseconds. long llo = lo*1000; if(lhi<1) lhi = 1; // make sure high and low times are < 1 millesecond. if(llo<1) llo = 1; astgpio(lhi, llo); // Pass high time and low time to Arduino simulation. } /******************************************************************** * monogpio() function - Run Arduino simulation of mono-stable mode. ********************************************************************/ void monogpio(long ondelay) { long ontime; int done = 0; if(ondelay > 30000) ondelay = 30000; // If light stays the same for over // thirty seconds it would be boring, // so change it after thirty seconds. digitalWrite(onpin, LOW); digitalWrite(offpin, HIGH); while(done==0) { while(digitalRead(button) == HIGH); // Wait for buttonpress long down = millis(); while(digitalRead(button) == LOW); // Debounce { if((millis() - down) > 1000) // If button held down for one second { // force end of Arduino simulation. while(digitalRead(button) == LOW); // Debounce done = 1; ondelay = 0; } } delay(10); ontime = millis(); down = millis(); while((millis() - ontime) < ondelay) { digitalWrite(onpin, HIGH); digitalWrite(offpin, LOW); if(digitalRead(button) == LOW) // If button pressed end Arduino simulation { if((millis() - down) > 1000) // If button held down for one second { // force end of Arduino simulation. while(digitalRead(button) == LOW); // Debounce done = 1; ondelay = 0; } } } digitalWrite(onpin, LOW); digitalWrite(offpin, HIGH); } digitalWrite(onpin, LOW); digitalWrite(offpin, LOW); } /******************************************************************** * mono() function - The code for a mono-stable "One Shot". ********************************************************************/ void mono() { float r1 = 0; float c1 = 0; Serial.println(F("555 Timer in Mono-Stable mode")); Serial.println(' '); Serial.print(F(" Enter R1: ")); Serial.flush(); while(Serial.available() == 0); if (Serial.available()) r1 = Serial.parseFloat(); Serial.println(r1); Serial.println(' '); if(mult==1000000000) Serial.print(F("Enter C1 in nF: ")); else if(mult==1000000) Serial.print(F("Enter C1 in uF: ")); else if(mult==1) Serial.print(F(" Enter C1 in F: ")); Serial.flush(); while(Serial.available() == 0); if (Serial.available()) c1 = Serial.parseFloat(); Serial.println(c1,5.5); Serial.println(' '); Serial.println(F("----------------------------------------")); Serial.println(' '); c1/=mult; float hi = 1.1 * r1 * c1; Serial.print(F("Time high = ")); Serial.println(hi,5); long lhi = hi*1000; // Convert high time from float to long milleseconds. if(lhi<1) lhi = 1; // Make sure it is not < 1 millesecond. monogpio(lhi); // Pass high time to Arduino simulation. } /******************************************************************** * capmult() function - Determine multiplier for capacitor value. ********************************************************************/ void capmult() { char choice = ' '; mult = 0; while(mult == 0) { Serial.println(F("Determine multiplier for capacitance")); Serial.println(F(" ")); Serial.println(F("1 - nano Farads (nF)")); Serial.println(F("2 - micro Farads (uF)")); Serial.println(F("3 - Farads (F)")); Serial.println(F(" ")); Serial.println(F("Enter Selection: ")); while(Serial.available() == 0); choice = Serial.read(); // Get character. if(choice == '1') mult = 1000000000; // Multiplier for nano Farads. else if(choice == '2') mult = 1000000; // Multiplier for micro Farads. else if(choice == '3') mult = 1; // Multiplier for Farads. } } /******************************************************************** * setup() required function ********************************************************************/ void setup() { pinMode(onpin, OUTPUT); // Set LED pins to output pinMode(offpin, OUTPUT); pinMode(button, INPUT_PULLUP); // Set button pin to input pullup digitalWrite(onpin, LOW); // Inital state of LEDs off digitalWrite(offpin, LOW); Serial.begin(115200); } /******************************************************************** * loop() required function ********************************************************************/ void loop() { char choice = ' '; Serial.println(F(" Calculator for 555 Timer")); Serial.println(F(" ")); Serial.println(F("1 - Astable Oscillator")); Serial.println(F("2 - Mono-stable")); Serial.print(F("3 - Change multiplier for C1 ")); if(mult==1) Serial.println(F("(F)")); else if(mult==1000000) Serial.println(F("(uF)")); else if(mult==1000000000) Serial.println(F("(nF)")); Serial.println(" "); Serial.print(F("Enter Selection: ")); while(Serial.available() == 0); choice = Serial.read(); // Get character. Serial.println(" "); Serial.println(" "); if(choice == '1') astable(); // Run code for astable oscillator. else if(choice =='2') mono(); // Run code for mono-stable "One Shot". else if(choice =='3') capmult(); // Get multiplier for capacitance. Serial.println(" "); Serial.println(" "); }