Skip to main content

Day 18: Dynamic Memory Allocation | Bluetooth

Lab

Background Info/Notes

  • Standardized protocol for sending/receiving data via 2.4GHz wireless link
  • Good for short-range, low-power, low-cost, wireless transmissions

How it Works

  • Use master/slave model
    • Master can send and request data from slaves
    • Slaves can only transmit to and receive from master
      • Can't talk to other slaves
  • Addresses and Names
    • Each device has unique 48-bit address
That's all theoretical. No matter what I did, I couldn't get the bluetooth to connect to my laptop. I tried resetting stuff, restarting my computer, using different code, using different hardware, nothing worked. I even took my laptop outside to try to connect it, but it simply wouldn't connect. 

Software: Dynamic Memory Allocation

Dynamic memory allocation allows for allocation of memory when program is executed instead of when it is compiled. Especially important when program uses array whose size is not determined until program is executed.

It is specified with either malloc or calloc function, which stand for "memory allocation" or "cleared allocation" and both reserve group of memory locations.

My cipher project from a previous homework assignment used dynamic memory allocation, so that it could reallocate memory according to the length of the user input.

Code:
#ifndef _CIPHERH_
#define _CIPHERH_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *getInput(void)
{
  int mem=16; 
  char *message=malloc(mem); //Preallocate memory
  printf("Input Message\n");
fgets(message,mem,stdin); //Take user input 

while(message[strlen(message)-1]!='\n') //Check if there's still space
{
mem*=2; 
message=realloc(message,mem); //Double amount of space
fgets(message + mem/2 - 1, mem/2 + 1, stdin); //Read the rest of the line
}
return message; //return character array message
}

int EncryptMessage(void)
{
  char *message=getInput(); //call getInput function
  int ch; //initialize variable ch
  
  int shift; //initialize variable shift
  
  printf("\nInput number of characters to shift: \n");
  scanf("%i",&shift); //ask user to choose shift for encryption
  
  char newarray[strlen(message)]; //initialize array that is the same length as the message to be encrypted
  
  for(int k=0; k<=strlen(message); k++)
  {
    ch=*(message+k); //set ch equal to the current value in the array
    if(ch>='a' && ch<='z')
    {
      ch=ch+shift; //encrypt letter
      if(ch>'z')
      {
        ch=ch-26; //if the value goes past z, subtract 26
      }
      if(ch<'a')
      {
        ch=ch+26; //if value goes past a, add 26;
      }
      newarray[k]=ch; //write new value into array
    }
    
    if(ch>='A' && ch<='Z')
    {
      ch=ch+shift;
      if(ch>'Z')
      {
        ch=ch-26;
      }
      if(ch<'A')
      {
        ch=ch+26;
      }
      newarray[k]=ch;
    }
    
    else
    {
      newarray[k]=ch; //ignore non-letters
      continue; //continue
    }
  }
  printf("Encrypted Message: \n %s\n",newarray); //print encrypted message
}

#endif

I used malloc to pre-allocate memory, and realloc to reallocate the memory. 

Comments

Popular posts from this blog

Day 25: Python, Graphing, 3D Visualization and Inertial Measurement

Python Arduino Code int trigPin=13; //Sensor Trig pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pin...

Day 19: Project 2

Our project was a test stand for rocket motors Using an LCD screen, we could choose a display mode, and it would display the selected value. Unfortunately, we were unable to calibrate the pressure sensor, so all we ended up with was a load cell. We used the HX711 library for this. The LCD screen displayed the maximum force experienced during the test Code for MotorSensors.h #ifndef _MOTORSENSORS_ #define _MOTORSENSORS_ #include <LiquidCrystal.h> //Include Liquid Crystal Library #include <HX711.h> //Include HX711 Load Cell library #include "HX711.h" //Include Load Cell header file #include <math.h> //Include math library #define calibration_factor -8815 //set calibration factor #define DAT 3 //define DAT pin #define CLK 2 //define CLK pin #define pressurePin A1 #define tempPin A2 HX711 scale(DAT,CLK);  LiquidCrystal lcd(8,9,4,5,6,7); //Define LCD pins //Initialize variables float maxkg=0;  float currentval=0; float maxforce=0; char mo...