Skip to content

Commit b95de73

Browse files
committed
Added DHT22 sensor to the codebase and the setup. You have to download the DHTlib from http://playground.arduino.cc//Main/DHTLib on your own, and copy it to ~/sketchbook/libraries.
Pin8 is used. New commands: d? -> sensor state; dt? and dh? are for temperature and humidity.
1 parent cd03421 commit b95de73

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* The String->float conversion: http://forum.arduino.cc/index.php?topic=179666.msg1331654#msg1331654
66
* LP Filter: http://sim.okawa-denshi.jp/en/PWMtool.php
77
* PWM speedup from here: http://playground.arduino.cc/Code/PwmFrequency
8+
* DHTlib, get it from here: http://playground.arduino.cc//Main/DHTLib
89

910
# Start Here:
1011
1. Clone the repository to your sketchbook folder.
@@ -29,6 +30,11 @@
2930
* ilk? -> GET interlock state (always 0)
3031
* f? -> GET operation fault state (always 0)
3132
* leds? -> GET led status (1: Power on, 7: Laser on)
33+
34+
# DHT22 Communication:
35+
* d? -> GET sensor status (0: error, 1: ok)
36+
* dt? -> GET temperature (float)
37+
* dh? -> GET humidity (float)
3238

3339
# Cobolt Laser Documentation
3440
* http://www.cobolt.se/wp-content/uploads/2014/10/Owners-Manual-05-01_140611.pdf

ldc205c/ldc205c.ino

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
A Controller for the Thorlabs LDC205C series equipment.
3+
4+
Main Functions:
5+
* ENABLE/DISABLE Laser -> Pin 13 + LED
6+
* SET Laser Current -> Pin 11 (Changed to 31 kHz)
7+
* GET Laser Current -> A0
8+
LDC205C -> k=50mA/V
9+
10+
Commands from Cobolt Gen5.
11+
12+
Power fit: P=AI+b
13+
B (y-intercept) = 2,158048764451327e+01 +/- 8,999878654085594e-02
14+
A (slope) = 6,979088703376868e-01 +/- 1,292424325833072e-03
15+
*/
16+
17+
// Please clone DHTlib from: https://github.com/RobTillaart/Arduino.git
18+
// Copy DHTlib to ~/sketchbook/libraries.
19+
#include <dht.h>
20+
dht DHT;
21+
22+
unsigned long serialNumber = 302432729;
23+
float time;
24+
25+
float A=0.69790887;
26+
float B=21.5804876;
27+
28+
float kLDC500 = 50.0; // mA/V
29+
30+
const int laserREM = 13;
31+
const int laserMod = 11;
32+
const int laserCTL = A0;
33+
const int dht22Pin = 8;
34+
35+
String inputString = ""; // a string to hold incoming data
36+
boolean stringComplete = false; // whether the string is complete
37+
38+
void setup() {
39+
pinMode(laserREM, OUTPUT);
40+
pinMode(laserMod, OUTPUT);
41+
// Change PWM Frequency for Timer2. f=~31kHZ
42+
// Copied the relevant part from here: http://playground.arduino.cc/Code/PwmFrequency
43+
TCCR2B = TCCR2B & 0b11111000 | 0x01;
44+
Serial.begin(115200);
45+
// reserve 200 bytes for the inputString:
46+
inputString.reserve(200);
47+
}
48+
49+
void setCurrent(float i) {
50+
// Lock to Max.
51+
if (i > 100.00) { i = 110.00; }
52+
int iDigit = i*1.535;
53+
analogWrite(laserMod, iDigit);
54+
Serial.println("OK\r");
55+
}
56+
57+
float getVoltage() {
58+
int ctlOut = analogRead(laserCTL);
59+
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
60+
return ctlOut*0.005; // voltage
61+
}
62+
63+
float getCurrent() {
64+
return getVoltage()*kLDC500;
65+
}
66+
67+
float getPower() {
68+
float p = (getCurrent()-B)/A;
69+
if ( p > 0 ) return p;
70+
else return 0.0;
71+
}
72+
73+
float toFloat(String s) {
74+
char carray[s.length() + 1]; //determine size of the array
75+
s.toCharArray(carray, sizeof(carray)); //put readStringinto an array
76+
float floatNumber = atof(carray); //convert the array into a float
77+
return floatNumber;
78+
}
79+
80+
void loop() {
81+
// get any incoming bytes:
82+
if (stringComplete) {
83+
// Status of 4 LEDs
84+
if (inputString.startsWith("leds?")) {
85+
// POWER ON + LASER ON + LASER LOCK + ERROR = 1 + 2 + 4 + 8
86+
int leds = 1+digitalRead(laserREM)*6;
87+
Serial.println(leds);
88+
// Laser ENABLE/DISABLE/STATE
89+
} else if (inputString.startsWith("l")) {
90+
if (inputString.substring(1,2) == "0") {
91+
digitalWrite(laserREM,LOW);
92+
Serial.println("OK\r");
93+
} else if (inputString.substring(1,2) == "1") {
94+
digitalWrite(laserREM,HIGH);
95+
Serial.println("OK\r");
96+
} else if (inputString.substring(1,2) == "?") {
97+
Serial.println(digitalRead(laserREM));
98+
}
99+
// Drive Current GET
100+
} else if (inputString.startsWith("i?")) {
101+
Serial.println(getCurrent());
102+
// Drive Current SET
103+
} else if (inputString.startsWith("slc")) {
104+
setCurrent(toFloat(inputString.substring(3)));
105+
// Power GET/SET
106+
} else if (inputString.startsWith("p")) {
107+
if ((inputString.substring(1,2) == "?") || (inputString.substring(1,3) == "a?")) {
108+
Serial.println(getPower());
109+
} else {
110+
float fp = toFloat(inputString.substring(1));
111+
float fpi;
112+
if ( fp < 1.0 ) fpi = A*fp*1000.0+B;
113+
else fpi = A*fp+B;
114+
if (fpi > 100.0) fpi = 100.0;
115+
setCurrent(fpi);
116+
}
117+
// DHT22 Sensor TEMP/HUM
118+
} else if (inputString.startsWith("d")) {
119+
int chk = DHT.read22(dht22Pin);
120+
if (inputString.substring(1,3) == "t?") {
121+
Serial.println(DHT.temperature, 1);
122+
} else if (inputString.substring(1,3) == "h?") {
123+
Serial.println(DHT.humidity, 1);
124+
} else if (inputString.substring(1,2) == "?") {
125+
if (chk == DHTLIB_OK) Serial.println("1");
126+
else Serial.println("0");
127+
}
128+
}
129+
// Serial Number
130+
} else if (inputString.startsWith("sn?")) {
131+
Serial.println(serialNumber);
132+
// Working seconds
133+
} else if (inputString.startsWith("hrs?")) {
134+
time = millis()/60000.0;
135+
Serial.println(time);
136+
// Interlock State, no real code behind
137+
} else if (inputString.startsWith("ilk?")) {
138+
Serial.println("0");
139+
// Operating fault GET, no real code behind
140+
} else if (inputString.startsWith("f?")) {
141+
Serial.println("0");
142+
} else {
143+
Serial.print("Syntax Error: ");
144+
Serial.println(inputString);
145+
}
146+
// clear the string:
147+
inputString = "";
148+
stringComplete = false;
149+
}
150+
}
151+
/*
152+
SerialEvent occurs whenever a new data comes in the
153+
hardware serial RX. This routine is run between each
154+
time loop() runs, so using delay inside loop can delay
155+
response. Multiple bytes of data may be available.
156+
*/
157+
void serialEvent() {
158+
while (Serial.available()) {
159+
// get the new byte:
160+
char inChar = (char)Serial.read();
161+
// add it to the inputString:
162+
inputString += inChar;
163+
// if the incoming character is a carriage return (ASCII 13),
164+
// set a flag so the main loop can do something about it:
165+
if (inChar == '\r') {
166+
stringComplete = true;
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)