Difference between revisions of "Arduino 101"
Jump to navigation
Jump to search
Line 44: | Line 44: | ||
= Project 2 = | = Project 2 = | ||
<pre> | <pre> | ||
+ | #include <SPI.h> | ||
+ | #include <SD.h> | ||
+ | int recordingButtonInputPin = 5; | ||
+ | int recordingLEDPin = 2; | ||
+ | boolean isRecording = false; | ||
+ | |||
+ | int ldrInputPin = 1; | ||
+ | |||
+ | int sdChipSelectPin = 7; | ||
+ | |||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | pinMode(recordingButtonInputPin, INPUT); | ||
+ | |||
+ | Serial.begin(9600); | ||
+ | while (!Serial) { | ||
+ | ; // wait for serial port to connect. Needed for native USB port only | ||
+ | } | ||
+ | Serial.print("Initializing SD card..."); | ||
+ | |||
+ | // see if the card is present and can be initialized: | ||
+ | if (!SD.begin(sdChipSelectPin)) | ||
+ | { | ||
+ | Serial.println("Card failed, or not present"); | ||
+ | // don't do anything more: | ||
+ | return; | ||
+ | } | ||
+ | Serial.println("card initialized."); | ||
+ | |||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | // put your main code here, to run repeatedly: | ||
+ | int recordNow = digitalRead(recordingButtonInputPin); | ||
+ | if (recordNow == HIGH) | ||
+ | { | ||
+ | delay(100); // Software debouncing of button | ||
+ | while(digitalRead(recordingButtonInputPin) == HIGH) { | ||
+ | ;// Wait for button release | ||
+ | } | ||
+ | if (isRecording == false) | ||
+ | { | ||
+ | isRecording = true; | ||
+ | digitalWrite(recordingLEDPin, HIGH); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | isRecording = false; | ||
+ | digitalWrite(recordingLEDPin, LOW); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (isRecording == true) | ||
+ | { | ||
+ | int lightStrength = analogRead(ldrInputPin); | ||
+ | File dataFile = SD.open("lightlog.txt", FILE_WRITE); | ||
+ | if (dataFile) | ||
+ | { | ||
+ | dataFile.println(String(lightStrength)); | ||
+ | dataFile.close(); | ||
+ | // print to the serial port too: | ||
+ | Serial.println(String(lightStrength)); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | // if the file isn't open, pop up an error: | ||
+ | Serial.println("error opening lightlog.txt"); | ||
+ | } | ||
+ | delay(1000); // Delay 1 second | ||
+ | } | ||
+ | |||
+ | } | ||
</pre> | </pre> | ||
+ | |||
= Project 3 = | = Project 3 = | ||
<pre> | <pre> | ||
</pre> | </pre> |
Revision as of 07:37, 7 November 2015
Project 1
int potInputPin = 0; int servoOutputPin = 11; int smallAngleLEDPin = 2; int largeAngleLEDPin = 4; int minValue = 64; int maxValue = 192; int analogValue = 512; void setup() { pinMode(servoOutputPin, OUTPUT); pinMode(smallAngleLEDPin, OUTPUT); pinMode(largeAngleLEDPin, OUTPUT); analogWrite(servoOutputPin, analogValue); Serial.begin(9600); } void loop() { analogValue = analogRead(potInputPin); Serial.println(analogValue); analogValue = analogValue / 4; Serial.println(analogValue); if (analogValue < minValue) { digitalWrite(smallAngleLEDPin, HIGH); } else if (analogValue > maxValue) { digitalWrite(largeAngleLEDPin, HIGH); } else { digitalWrite(smallAngleLEDPin, LOW); digitalWrite(largeAngleLEDPin, LOW); analogWrite(servoOutputPin, analogValue); } delay(100); }
Project 2
#include <SPI.h> #include <SD.h> int recordingButtonInputPin = 5; int recordingLEDPin = 2; boolean isRecording = false; int ldrInputPin = 1; int sdChipSelectPin = 7; void setup() { pinMode(recordingButtonInputPin, INPUT); Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.print("Initializing SD card..."); // see if the card is present and can be initialized: if (!SD.begin(sdChipSelectPin)) { Serial.println("Card failed, or not present"); // don't do anything more: return; } Serial.println("card initialized."); } void loop() { // put your main code here, to run repeatedly: int recordNow = digitalRead(recordingButtonInputPin); if (recordNow == HIGH) { delay(100); // Software debouncing of button while(digitalRead(recordingButtonInputPin) == HIGH) { ;// Wait for button release } if (isRecording == false) { isRecording = true; digitalWrite(recordingLEDPin, HIGH); } else { isRecording = false; digitalWrite(recordingLEDPin, LOW); } } if (isRecording == true) { int lightStrength = analogRead(ldrInputPin); File dataFile = SD.open("lightlog.txt", FILE_WRITE); if (dataFile) { dataFile.println(String(lightStrength)); dataFile.close(); // print to the serial port too: Serial.println(String(lightStrength)); } else { // if the file isn't open, pop up an error: Serial.println("error opening lightlog.txt"); } delay(1000); // Delay 1 second } }