Posts

Lab 20/ HW 20

Lab 20 (SD Reader w/ Temp Data): /Write to SD #include <SD.h> #include <Wire.h> int temp_address = 72; long previousMillis = 0; int interval = 1000; volatile long int timemax = 60000; const int button1 = 2; const int button2 = 3; //Set by default for the SD Card Library //MOSI = pin 11 //MISO = pin 12 //SCLK = pin 13 //We always need to set the CS Pin const int CS_PIN = 10; void setup() {   Serial.begin(9600);   attachInterrupt(digitalPinToInterrupt(button1), increase, RISING);   attachInterrupt(digitalPinToInterrupt(button2), decrease, RISING);    Wire.begin();   Serial.println("Initializing Card");   //CS pin is an output   pinMode(CS_PIN, OUTPUT);   if (!SD.begin(CS_PIN))   {     Serial.println("Card Failure");     return;   }   Serial.println("Card Ready"); } void increase() {   timemax = timemax + 1000;   Serial.print("New Time Interval: ");   Serial.println(timemax); } void...

Lab 19/ HW 19

Lab 19 (RTC): // Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include <RTClib.h> #include <EEPROM.h> #include <math.h> RTC_DS1307 rtc; int years = 0, previousyears = 0, elapsedyears = 0; int months = 0, previousmonths = 0, elapsedmonths = 0; int days = 0, previousdays = 0, elapseddays = 0; int hours = 0, previoushours = 0, elapsedhours = 0; int minutes = 0, previousminutes = 0, elapsedminutes = 0; int seconds = 0, previousseconds = 0, elapsedseconds = 0; void setup () { Serial.begin(57600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } DateTime now = rtc.now(); //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: //rtc.adjust(DateTime(2018, 11, 9, 12, 26, 00)); previousyears = EEPROM.read(1); previousmonths = EEPROM.read(2); previousdays = EEPROM.read(3); pr...

Lab 18/ HW 18

Lab 18 (Bluetooth): HW 18 (Genome): /* This program initializes a long character string and a short */ /* character string. It then prints the locations of the short */ /* string in the long string. It also prints the number of */ /* occurrences of the short string in the long string. */ #include <stdio.h> #include <string.h> #define SIZE 1000 #define FILENAME "string.txt" int main(void) { /* Declare and initialize variables. */ int count=0; char long_str[SIZE], short_str[SIZE]; char *ptr1=long_str, *ptr2=short_str; FILE *string; string = fopen(FILENAME, "r"); if (string == NULL) { printf("There is no file to read from.\n"); printf("Enter letters for the Long String:\n"); scanf("%s", &long_str); printf("Enter letters for the Short String:\n"); scanf("%s", &short_str);  /* Count the number of occurrences of short_str in long_str. */ /* While the function strstr does not return NULL, increment ...

Lab 17/ HW 17

Lab 17 (Robot w/ Joystick): Lab 17b (Interrupts): //Use Hardware-Debounced Switch to Control Interrupt //Button pins const int BUTTON_INT = 0; //Interrupt 0 (pin 2 on the Uno) const int PHOTO_RED = 1; const int RED =11; //Red LED on pin 11 const int GREEN =10; //Green LED on pin 10 const int BLUE =9; //Blue LED on pin 9 int photo = 0; //Volatile variables can change inside interrupts volatile int selectedLED = GREEN; void setup() { pinMode (RED, OUTPUT); pinMode (GREEN, OUTPUT); pinMode (BLUE, OUTPUT); //The pin is inverted, so we want to look at the rising edge attachInterrupt(BUTTON_INT, swap, RISING); attachInterrupt(PHOTO_RED, red, FALLING); } void swap() { //Turn off the current LED analogWrite(selectedLED, 0); //Then, choose a new one. if (selectedLED == GREEN) selectedLED = RED; else if (selectedLED == RED) selectedLED = BLUE; else if (selectedLED == BLUE) selectedLED = GREEN; } void red() {   analogWrite(selectedLED, 0);   selectedLED = RED; } void loop() { for (...

Lab 16/ HW 16

Lab 16 (H-Bridge + Dual Motors): const int MC1 = 9; //Motor Control 1 const int MC2 = 10; //Motor Control 2 int incr_velocity = 150; int velocity = 0; int speed = 0; int mid_val = 512; void setup() {   pinMode(MC1, OUTPUT);   pinMode(MC2, OUTPUT);   Serial.begin(9600);   Serial.println("Enter number for control option:");   Serial.println("1. STOP");   Serial.println("2. FORWARD");   Serial.println("3. REVERSE");   Serial.println("4. READ CURRENT");   Serial.println("+. INCREASE SPEED");   Serial.println("-. DECREASE SPEED"); } void loop() { //Serial.println(mid_val); if (mid_val > 1023) {   mid_val = 1023; } if (mid_val < 0) {   mid_val = 0; } if (mid_val >= 512)   {     velocity = map(mid_val, 512, 1023, 20, 255);     constrain(velocity, 0, 255);     forward(velocity);   } else if (mid_val <= 512)   {     velocity = ma...