Lab Day 5

Lecture 7/11

Today in lecture, we explored basic arduino programming. The first circuit we set up was call a pullup circuit. This is where pressing on the switch turns the LED on. Next, we switched it to a pulldown circuit. This is where pressing on the switch turns the LED off. We also learned about conditional statements, logical operations, and for loops. Conditional statements are something that I was familiar with as my initial prototype (click here) used conditional statemtns. For my prototype the conditional statements were “if” something about the temperature is true (for example, it is 5 C higher than the baseline), then certain LED(s) would turn on. I was not familar with logical operations nor for loops. Logical operations use different symbols, such as ampersands, to enable more than one expression to be considered at the same time. For loops are used for repetitive commands.







Lab 7/12

Today in lab, our assigment was to program a microcontroller to produce an output using an input. Additionally, there needed to be a conditional statement and a for loop in the code. My idea was to program a motor so that when the button was not pressed, the speed of the motor would accelerate to its maximum and then go back to roughly no speed and accelerate again (this part was programmed by the for loop). Next, when the button is pressed the motor will spin at its constant maximum speed. Originally, I wanted to have the motor speed up due to the time elapsed while the button was pressed. This, however, was quite challenging as even when I nested the for loop code inside of the conditional button state code, the button state seemed to override the accleration that was coded to occur due to the for loop. I used the serial monitor to check the state of the button to see if this was the problem. The button was working as when I pressed it on the serial monitor read 1 and when I was not pressing it, it read 0.

It also took a bit of trial and error to make the motor acclerate. Originally, I had a % in my for loop. This was causing a problem because for the instances where the expression was false, the motor would stop. This produced an unsual, and somewhat irregular, motion of the motor as it kept starting and stoping.


const int PWMpin = 10;

int buttonState = 0;

void setup() {

  pinMode(motorPin, OUTPUT);
  pinMode(motorPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  
  buttonState = digitalRead(buttonPin);
  
    Serial.println(buttonState);
  if (buttonState == HIGH) {
    Serial.println(buttonState);
    for (int i = 0; i <= 255; i++) {
      analogWrite(PWMpin, i);
      delay(10);
    }
  }
}