Skip to content
Snippets Groups Projects

Shutter rework

Merged Benedikt Burger requested to merge ShutterRework into master
1 file
+ 120
431
Compare changes
  • Side-by-side
  • Inline
//-----------------------------------------Activate LCD--------------------------------------
//#include <LiquidCrystal.h> //Library for LCD
//LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Initialize LCD Lib for our application //the numbers are the Digital Pins, which are used for the LCD
/*
****************************************
* Control the shutters of the Quanta-Ray
****************************************
*/
//-----------------------------------------Activate W-Lan Communication--------------------------------------
const String version = "Shutterschalter 0.1"; // Version of this script
//#include <ArduinoJson.h> //Library for W-Lan Communication
//#define SSID "PI_AP" //Service Set Identifier
//#define PASSWORD "SQV10500"
//#define ip "192.168.42.1" //
//#define LED_WLAN 13
#include <LiquidCrystal.h> //Library for LCD
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Initialize LCD Lib for our application //the numbers are the Digital Pins, which are used for the LCD
#define DEBUG true
//#include <SoftwareSerial.h> //Library for the W-Lan module
//SoftwareSerial esp8266(11, 12); // RX, TX, Digital Pins required for the W-Lan module
// PINs
const int fundamentalSwitchPin = 6;
const int fundamentalShutterPin = A0;
const int fundamentalLedPin = A1;
const int shgSwitchPin = 7;
const int shgShutterPin = 9;
const int shgLedPin = 8;
//-----------------------------------------Define variables and pins--------------------------------------
// Constants
const int HighLimit = 850;
const float refVoltage=5; //reference voltage for analog ins. If a different voltage is applied to A_ref pin, specify here.
// State variables
int fundamentalShutter = LOW;
int shgShutter = LOW;
bool emergency = false;
const int analogInputs[]={3,4,5,6,7}; //
const int digitalInputs[]={10,13,A1,A2};
const int digitalOutputs[]={8,9,A0};
int fundamentalSwitch = LOW;
int shgSwitch = LOW;
int shutter532 = LOW; //Shutter write state for green light (532nm)
int shutter355 = LOW; //Shutter write state for ultraviolett light (355nm)
float Power532 = 0.0; //Measured Power at 532 nm
float Power355 = 0.0; //Measured Power at 355 nm
void setup(){
//Setup the serial connection and the pins
Serial.begin(9600);
int shutter532Pin = 9; //Shutter write pin for green light (532nm)
const int ledPin532 = 8; //Shutter LED pin for green light (532nm)
int ledState532 = LOW; //Shutter LED state for green light (532nm)
//Missing parts for 532, will not be added
// Setup pins
pinMode(fundamentalShutterPin, OUTPUT);
pinMode(fundamentalLedPin, OUTPUT);
pinMode(shgShutterPin, OUTPUT);
pinMode(shgLedPin, OUTPUT);
int shutter355Pin = A0; //Shutter write pin for UV light (355nm)
const int ledPin355 = A1; //Shutter LED pin for UV light (355nm)
int ledState355 = LOW; //Shutter LED state for UV light (355nm)
const int AIMPin355 = A3; //Shutter AIM pin for UV light (355nm)
int AIMState355 = LOW;//Shutter AIM state for UV light (355nm)
const int OPENPin355 = A2;//Shutter OPEN pin for UV light (355nm)
int OPENState355 = LOW;//Shutter OPEN state for UV light (355nm)
unsigned long oldmillis = 0; //timing constant 355nm
unsigned long oldmillis532 = 0; //timing constant 532nm
unsigned long previousMillis = 0;
const long interval = 1500;
const long intervalemergency = 500;
const long intervalrasp = 5000;
//-----------------------------------------Setup runs once--------------------------------------
void setup() {
//set reference voltage for analog ins
if (refVoltage==5)
analogReference(DEFAULT);
else
analogReference(EXTERNAL);
for (int i=0; i<sizeof(digitalInputs)/sizeof(int); i++){
pinMode(digitalInputs[i],INPUT_PULLUP);}
for (int i=0; i<sizeof(digitalOutputs)/sizeof(int); i++){
pinMode(digitalOutputs[i],OUTPUT);}
// Serial.begin(19200); //Start serial connection to computer
// Serial.println("Starting");
// esp8266.begin(19200); //start W-Lan module
// if (!espConfig()) serialDebug();
// else digitalWrite(LED_WLAN, HIGH);
// sendCom("AT+CIPMODE=0");
//
// lcd.begin(16, 2); //Initialize LCD
lcd.begin(16, 2);
//lcd.noCursor(); // hide the cursor
lcd.print("Fund CLOSED");
lcd.setCursor(0, 1);
lcd.print("SHG CLOSED");
}
//-----------------------------------------Loop runs continously--------------------------------------
void loop() {
unsigned long currentMillis = millis();
//--------------------------------Determine Analog in A6 value (Shutter Control Value 355)---------------------------
// lcd.setCursor(0, 1);//Place on the LCD
int resettime = millis() / 1000;
delay(2);
int ShutterControlValue355 = analogRead(6); //Read, if one switch is toggled 355nm
// lcd.print(ShutterControlValue355);//lcd.print(ReadAnalog(6,1000));//Print on LCD
// lcd.setCursor(13, 1);//Place on the LCD
// lcd.print(resettime);//Print on LCD
delay(2);
//--------------------------------Determine Analog in A7 value (Shutter Control Value 532)---------------------------
// lcd.setCursor(0, 0);//Place on the LCD
// delay(2);
int ShutterControlValue532 = analogRead(7); //Read, if one switch is toggled 532nm
// delay(2);
// lcd.print(ShutterControlValue532);//Print on LCD
delay(2);
//--------------------------------React on Shutter Control Value 355---------------------------
if(ShutterControlValue355>850 && shutter532==LOW) //1.: >850 value for shutter open, 2. Shutter 532nm should be closed! You don´t need both, or?
{shutter355 = HIGH;
digitalWrite(shutter355Pin,shutter355);
// lcd.setCursor(4, 1);
// lcd.print("355open");
// if(currentMillis-oldmillis>10000){sendRaspPost("NdYAG Pro 355nm", "experiment");
// sendRaspPost("PDA", "experiment");
// oldmillis=millis();}}
// else if (ShutterControlValue355<600)
// {shutter355 = LOW;
// digitalWrite(shutter355Pin,shutter355);
// lcd.setCursor(4, 1);
// lcd.print("Emergency");
}
else{shutter355 = LOW;
digitalWrite(shutter355Pin,shutter355);
// lcd.setCursor(4, 1);
// lcd.print("355closed");
}
delay(2);
//--------------------------------React on Shutter Control Value 532---------------------------
if(ShutterControlValue532>850 && shutter355==LOW) //1.: >850 value for shutter open, 2. Shutter 355nm should be closed! You don´t need both, or?
{shutter532 = HIGH;
digitalWrite(shutter532Pin,shutter532);
// if(currentMillis-oldmillis532>10000){sendRaspPost("NdYAG Pro 532nm", "active");
// sendRaspPost("Regen", "active");}
// lcd.setCursor(7, 0);}
// else if (ShutterControlValue532<600)
// {shutter532 = LOW;
// digitalWrite(shutter532Pin,shutter532);
// lcd.setCursor(7, 0);
//lcd.print("Emergency");
}
else{shutter532 = LOW;
digitalWrite(shutter532Pin,shutter532);
// lcd.setCursor(7, 0);
//lcd.print("532closed");
}
delay(2);
//--------------------------------Read photodiode and communicate the value---------------------------
float PDvalue = ReadAnalog(5,5000);
Power532 = PDvalue * (5.0/1023.0);
// delay(2);
// lcd.setCursor(8, 0);
// lcd.print("PD");
// lcd.println(Power532);
delay(2);
// Serial.println(Power532);
// Serial.println(analogRead(5));
//--------------------------------React on photodiode value---------------------------
if(Power532<0.9)
{//shutter532 = HIGH;
// digitalWrite(shutter532Pin,shutter532);
// Serial.print("shutter532 Status: ");
// Serial.println(shutter532);
//lcd.setCursor(4, 1);
//lcd.print("532 ");
// lcd.setCursor(8, 1);
// lcd.print("On ");
void loop(){
// Do serial communication first
if ( Serial.available() ){
char input[20];
int len;
len = Serial.readBytesUntil('\n', input, 20);
input[len] = '\0'; // Null terminating the array to interpret it as a string
switch (input[0]){
case 'p': // Ping
Serial.println("Pong");
break;
case 's': // Read the shutters
Serial.print("FUND ");
if ( fundamentalShutter == HIGH)
Serial.print("1");
else
Serial.print("0");
Serial.print("\t");
Serial.print("SHG ");
if ( shgShutter == HIGH)
Serial.print("1");
else
Serial.print("0");
Serial.println();
break;
case 'l': // list valaues
Serial.print(fundamentalSwitch);
Serial.print("\t");
Serial.print(shgSwitch);
Serial.println();
break;
case 'd': // debug
Serial.println("Debug");
break;
case 'v': // Send Version
Serial.println(version);
break;
}
else{shutter532 = LOW;
digitalWrite(shutter532Pin,shutter532);
// lcd.setCursor(0, 1);
// lcd.print("Emergency close");
delay(10000000000);
}
if(Power532>0.03)
{if (currentMillis - previousMillis >= intervalrasp) {
// save the last time you send information
previousMillis = currentMillis;
// sendRaspPost("NdYAG Pro 532nm", "experiment");
delay(2);
if (ShutterControlValue355<850)
{shutter355 = LOW;
digitalWrite(shutter355Pin,shutter355);}
if (ShutterControlValue532<850)
{shutter532 = LOW;
digitalWrite(shutter532Pin,shutter532);}
// sendRaspPost("Regen", "experiment");
}}
else{}
//-----------------------------------------Communication with raspberry--------------------------------------
//Possible Lasers: "NdYAG Pro 1064nm","NdYAG Pro 532nm","NdYAG Pro 355nm","PDA","Regen"
//Possible Status: "inactive","blocked",
if (ShutterControlValue532<850)
{shutter532 = LOW;
digitalWrite(shutter532Pin,shutter532);}
//sendRaspPost("NdYAG Pro 532nm", "active");
delay(2);
}//End of the loop
//-----------------------------------------own Functions--------------------------------------
//-----------------------------------------averaged analog Read--------------------------------------
float ReadAnalog(int input, int avg)
{
float sensvalue=0; // read the sensors
for(int x=1; x<=avg; x++){ // repeat measurement 'numberOfValuesToAverage' times
sensvalue += analogRead(input); //take value
}
sensvalue=sensvalue/avg;
return sensvalue;
}
//-----------------------------------------Measuring flowrates (Rectangle Voltage)--------------------------------------
float Flowrate(int input, int avg, unsigned long timeout){
unsigned long starttime=micros();
unsigned long endtime=micros();
boolean startState=digitalRead(input);
// Serial.print("Reading FlowSensor:");
// Serial.println(digitalRead(input));
// Serial.println("starttime=");
// Serial.println(starttime);
// Serial.println("timediff=");
// Serial.println((micros()-starttime));
// Serial.println(digitalRead(input)==startState);
while ((micros()-starttime)<=timeout && digitalRead(input)==startState){
endtime=micros();
// Serial.print("Reading FlowSensor:");
// Serial.println(digitalRead(input));
}
// Do shutter control Fundamental
fundamentalSwitch = analogRead(fundamentalSwitchPin);
if (fundamentalSwitch > HighLimit && fundamentalShutter == LOW){
fundamentalShutter = HIGH;
digitalWrite(fundamentalShutterPin, HIGH);
lcd.setCursor(5, 0);
lcd.print("OPEN ");
}
else if (fundamentalSwitch < HighLimit && fundamentalShutter == HIGH){
fundamentalShutter = LOW;
digitalWrite(fundamentalShutterPin, LOW);
lcd.setCursor(5, 0);
lcd.print("CLOSED");
}
// Do shutter control SHG
shgSwitch = analogRead(shgSwitchPin);
if (shgSwitch > HighLimit && shgShutter == LOW){
shgShutter = HIGH;
digitalWrite(shgShutterPin, HIGH);
lcd.setCursor(5, 1);
lcd.print("OPEN ");
}
else if (shgSwitch < HighLimit && shgShutter == HIGH){
shgShutter = LOW;
digitalWrite(shgShutterPin, LOW);
lcd.setCursor(5, 1);
lcd.print("CLOSED");
}
// Show emergency off
if ( shgSwitch < 10 and ! emergency){
emergency = true;
lcd.setCursor(5, 0);
lcd.print("EMERGENCY");
lcd.setCursor(5, 1);
lcd.print("EMERGENCY");
}
else if ( shgSwitch > 10 and emergency ){
emergency = false;
lcd.setCursor(5, 0);
lcd.print("CLOSED ");
lcd.setCursor(5, 1);
lcd.print("CLOSED ");
}
}
unsigned long deltatime=endtime-starttime;
float flow=500000.0/deltatime;
return flow;
}
//-----------------------------------------Alarm sound (inactive)--------------------------------------
//void alarm(int SENSOR, int value, int PIEZO)
//{ debug("alarm!");
// while (analogRead(SENSOR) >= 500)
// {
// tone(PIEZO, 400, 500);
// delay(500);
// tone(PIEZO, 800, 500);
// delay(500);
// }
//}
//-----------------------------------------Basic communication between arduino and raspberry--------------------------------------
//boolean sendRaspPost(String laserName, String laserStatus)
//{
// boolean succes = true;
// String Host = ip;
// String msg = "field1=" + String("message");
// succes &= sendCom("AT+CIPSTART=\"TCP\",\"" + Host + "\",12345", "OK");
//
// String postRequest = JsonMessage(laserName,laserStatus);
//
// if(succes)
// {
// if (sendCom("AT+CIPSEND=" + String(postRequest.length()), ">"))
// {
// esp8266.print(postRequest);
// esp8266.find("SEND OK");
// int i=0;
// for (i=1;i<100;i++){ if(esp8266.available())
// Serial.write(esp8266.read());}
// if (!esp8266.find("CLOSED")) succes &= sendCom("AT+CIPCLOSE", "OK");
// }
// else
// {
// succes = false;
// }
// }
// return succes;
//}
String JsonMessage(String laserName, String laserStatus){
//
// Step 1: Reserve memory space
//
//StaticJsonBuffer<200> jsonBuffer;
//
// Step 2: Build object tree in memory
//
//JsonObject& root = jsonBuffer.createObject();
//root["name"] = laserName;
//root["status"] = laserStatus;
//JsonArray& data = root.createNestedArray("data");
//data.add(48.756080, 6); // 6 is the number of decimals to print
//data.add(2.302038, 6); // if not specified, 2 digits are printed
//
// Step 3: Generate the JSON string
//
char buff[256];
//root.printTo(buff, sizeof(buff));
return buff;
}
//-----------------------------------------Config ESP8266------------------------------------
//boolean espConfig()
//{
// boolean succes = true;
//
// esp8266.setTimeout(5000);
// succes &= sendCom("AT+RST", "ready");
// esp8266.setTimeout(1000);
//
// if (configStation(SSID, PASSWORD)) {
// succes &= true;
// debug("WLAN Connected");
// debug("My IP is:");
// debug(sendCom("AT+CIFSR"));
// }
// else
// {
// succes &= false;
// }
//shorter Timeout for faster wrong UPD-Comands handling
// succes &= sendCom("AT+CIPMUX=0", "OK");
// succes &= sendCom("AT+CIPMODE=0", "OK");
//
// return succes;
//}
//
//boolean configTCPServer()
//{
// boolean succes = true;
//
// succes &= (sendCom("AT+CIPMUX=1", "OK"));
// succes &= (sendCom("AT+CIPSERVER=1,80", "OK"));
//
// return succes;
//
//}
//
//boolean configTCPClient()
//{
// boolean succes = true;
//
// succes &= (sendCom("AT+CIPMUX=0", "OK"));
//succes &= (sendCom("AT+CIPSERVER=1,80", "OK"));
// return succes;
//}
//boolean configStation(String vSSID, String vPASSWORT)
//{
// boolean succes = true;
// succes &= (sendCom("AT+CWMODE=1", "OK"));
// esp8266.setTimeout(20000);
// succes &= (sendCom("AT+CWJAP=\"" + String(vSSID) + "\",\"" + String(vPASSWORT) + "\"", "OK"));
// esp8266.setTimeout(1000);
// return succes;
//}
//
//boolean configAP()
//{
// boolean succes = true;
//
// succes &= (sendCom("AT+CWMODE=2", "OK"));
// succes &= (sendCom("AT+CWSAP=\"NanoESP\",\"\",5,0", "OK"));
//
// return succes;
//}
//
//boolean configUDP()
//{
// boolean succes = true;
//
// succes &= (sendCom("AT+CIPMODE=0", "OK"));
// succes &= (sendCom("AT+CIPMUX=0", "OK"));
// succes &= sendCom("AT+CIPSTART=\"UDP\",\"192.168.255.255\",90,91,2", "OK"); //Importand Boradcast...Reconnect IP
// return succes;
//}
//-----------------------------------------------Controll ESP-----------------------------------------------------
//boolean sendUDP(String Msg)
//{
// boolean succes = true;
//
// succes &= sendCom("AT+CIPSEND=" + String(Msg.length() + 2), ">"); //+",\"192.168.4.2\",90", ">");
// if (succes)
// {
// succes &= sendCom(Msg, "OK");
// }
// return succes;
//}
//
//
//boolean sendCom(String command, char respond[])
//{
// esp8266.println(command);
// if (esp8266.findUntil(respond, "ERROR"))
// {
// return true;
// }
// else
// {
// debug("ESP SEND ERROR: " + command);
// return false;
// }
//}
//
//String sendCom(String command)
//{
// esp8266.println(command);
// return esp8266.readString();
//}
//-------------------------------------------------Debug Functions------------------------------------------------------
//void serialDebug() {
// while (true)
// {
// if (esp8266.available())
// Serial.write(esp8266.read());
// if (Serial.available())
// esp8266.write(Serial.read());
// }
//}
//
//void debug(String Msg)
//{
// if (DEBUG)
// {
// Serial.println(Msg);
// }
//}
Loading