Arduino 101

From LVL1
Revision as of 22:38, 18 November 2015 by Notyou007a (talk | contribs)
Jump to navigation Jump to search

Presentation

https://docs.google.com/presentation/d/1sB8RygR-oZyq8jZwiidcsYhblC1duVVDcdwAD0Z3vJI/edit?usp=sharing

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
  }

}