Machine safety Blog #6
I am doing this update because I went out of my comfort zone with programming. Years ago this wouldn't have been such a big deal for myself and I'm sure lots of you reading this could do it in your sleep. For me, I'm kind of proud of what I did.
So my issue was I wanted to test the sensor on other things besides machinery. Just a simple program to read the sensor. So I was loading the Arduino with the simple code then reloading it with the machinery safety code. Back and forth over and over. To solve this I had 2 options, run 2 Arduino's or create a program that gives you a choice on which program you want to run. I did the 2nd choice.
Below is the program that allows you to have a choice of which program you want to run:
#include <LiquidCrystal.h> const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); int vib = A0; //Vibration sensor pin int safelt1 = 8; //Assign warning LED int safelt2 = 9; //Assign warning LED int warnlt1 = 10; //Assign warning LED int warnlt2 = 13; //Assign warning LED int shutdwnlt = 14; //Assign warning LED int shutdwnsw = 7; //Assign shutdown switch int data; //Sensor reading int average = 0; //Average reading int averageA = 0; int averageB = 0; int x = 0; //Loop count int y = 0; //Creates infinate loop int tread = 0; //Temporary read int hread = 0; //Highest reading const byte modeButton1 = 18; const byte modeButton2 = 19; void setup(){ pinMode(modeButton1, OUTPUT); pinMode(modeButton2, OUTPUT); pinMode(shutdwnsw, OUTPUT); digitalWrite(shutdwnsw, LOW); digitalWrite(modeButton1, LOW); digitalWrite(modeButton2, LOW); lcd.clear(); lcd.begin(16, 2); lcd.setCursor(3,0); lcd.print("Vibration"); lcd.setCursor(5,1); lcd.print("Sensor"); delay(3000); pinMode(vib, INPUT); //initialize vib sensor as INPUT Serial.begin(9600); //begin the serial monitor at 9600 baud lcd.clear(); } void loop(){ lcd.setCursor(0,0); lcd.print("A-Run Program"); lcd.setCursor(0,1); lcd.print("B-Run Test"); // if button A pressed if(digitalRead(modeButton1) == HIGH) { programOne(); } // if button B pressed if(digitalRead(modeButton2) == HIGH) { testTwo(); } } //Run the vibration program void programOne() { pinMode(vib, INPUT); //initialize vib sensor as INPUT Serial.begin(9600); //begin the serial monitor at 9600 baud lcd.clear(); do{ //Calibrate average vibration x = x + 1; averageA = analogRead(vib); averageB = averageB + averageA; Serial.println("Calibrating"); lcd.setCursor(0,0); lcd.print("Calibrating"); lcd.setCursor(0,1); lcd.print("XXXXXXXXXXXXXXX " + x); delay(500); } while (x < 15); average = averageB/15; do{ delay(100); data=analogRead(vib); Serial.println(data); digitalWrite(safelt1, HIGH); tread = data; // Set highest reading if(hread < tread) { hread = tread; } if(data <= (average*1.05)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Within Limits"); lcd.setCursor(0,0); lcd.print("Ave="); // Average lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); //Highest recorded lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("--------"); lcd.setCursor(10,0); lcd.print("CR="); //Current recorded lcd.setCursor(13,0); lcd.print(data); } else if(data <= (average*1.1)){ lcd.clear(); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Within Limits High"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XX------"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); } else if(data <= (average*1.2)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Warning Limits"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XXXX----"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); } else if(data <= (average*1.35)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Warning Limits High"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XXXXXX--"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); digitalWrite(warnlt2, HIGH); } else if(data <= (average*1.5)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Shut Down Required"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XXXXXXXX"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); digitalWrite(warnlt2, HIGH); digitalWrite(shutdwnlt, HIGH); } else if(data > (average*1.5)){ Serial.println("Shutting Down"); lcd.clear(); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); digitalWrite(warnlt2, HIGH); digitalWrite(shutdwnlt, HIGH); digitalWrite(shutdwnsw, HIGH); lcd.setCursor(1,0); lcd.print("Limit Exceeded"); lcd.setCursor(8,1); lcd.print("Max="); lcd.setCursor(12,1); lcd.print(data); delay(5000); lcd.clear(); lcd.setCursor(0,0); lcd.print("Press Button to"); lcd.setCursor(2,1); lcd.print("Reset Sensor"); while(0 == 0); // Causes infinate loop {} } delay(100); }while (y == 0); } //Test the vibration sensor void testTwo() { do{ //Calibrate average vibration x = x + 1; averageA = analogRead(vib); averageB = averageB + averageA; Serial.println("Calibrating"); lcd.setCursor(0,0); lcd.print("Calibrating"); lcd.setCursor(0,1); lcd.print("XXXXXXXXXXXXXXX " + x); delay(500); } while (x < 15); average = averageB/15; do{ delay(100); data=analogRead(vib); Serial.println(data); digitalWrite(safelt1, HIGH); tread = data; // Set highest reading if(hread < tread) { hread = tread; } lcd.clear(); lcd.setCursor(0,0); lcd.print("Ca="); lcd.setCursor(3,0); lcd.print(average); //Display average vibration lcd.setCursor(8,0); lcd.print("CR="); lcd.setCursor(11,0); lcd.print(data); //Displays current vibrations lcd.setCursor(8,1); lcd.print("HR="); lcd.setCursor(11,1); lcd.print(hread); //Displays highest vibration reading delay(100); }while (y == 0); }
Like I said, it may not be anything new, but it was for me and it works!!
Dale Winhold