-
Notifications
You must be signed in to change notification settings - Fork 5
/
O2simSD.ino
74 lines (63 loc) · 1.3 KB
/
O2simSD.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
O2 Sensor Simulator with Data Logger
Code by Hiruna Wijesinghe
Last Modified: 231115 @ 1812
*/
#include <SPI.h>
#include <SD.h>
/*
Not required to initialize as and input or output
This is predefined in the SD Library.
MOSI = Pin 11
MISO = Pin 12
SCLK = PIN 13
*/
int CSP = 10;
int count = 0;
int voltage = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Initializing Card");
pinMode(CSP, OUTPUT);
pinMode(8, OUTPUT); //to draw power to card
digitalWrite(8, HIGH);
pinMode(9,OUTPUT); //voltage out to the ECU
pinMode(5,INPUT); //potentiometer pin
if (!SD.begin(CSP))
{
Serial.println("Error");
return;
}
Serial.println("OK");
}
void loop()
{
File sensorLog = SD.open("log.txt", FILE_WRITE);
if (sensorLog)
{
voltage = analogRead(5);
Serial.println(voltage);//DEBUG
voltage = map(voltage, 0, 1023, 0, 50); //value 0 and 50 can be changed according to the desired voltage output
Serial.print("new voltage: ");
Serial.println(voltage);//DEBUG
analogWrite(9,voltage);
sensorLog.print(count);
sensorLog.print(",");
sensorLog.println(voltage);
sensorLog.close();
count++;
/*
SAMPLE OUTPUT TO FILE:
0,23
0,23
0,23
0,41
*/
}
else
{
Serial.println("File read error");
}
delay(500);
}