Skip to main content

Day 8: Intro to Data Structures | Servos and More Music

Lecture

One Dimensional Arrays

Arrays are a type of data structure that allows for working with a group of values using a single identifier. 

Lab

Servos

Servos rotate to a particular angular position and stay there until told to move to a new position. 

There are both standard and continuous rotation servos. Unmodified ones have a fixed range due to a potentiometer in line with the drive shaft that is used for reporting the present position. Servos are controlled by sending a pulse of a certain length; this length determines the position it will rotate to. However, in a continuous rotation servo, there is no potentiometer and the servo rotates continuously, and the pulse length determines the speed instead. 

Servos draw a significant amount of current, typically more than an Arduino can supply, so it is best to use a separate power supply.

Homework

Hardware


//Servo Potentiometer Control
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int pir=2; //pir on pin 2
Servo myServo;
int val = 0; //for storing the reading from the POT

void setup()
  {
   myServo.attach(SERVO);
   myServo.write(90);
  }

void loop()
  {
   val = digitalRead(pir); //Read pir
   if(val==HIGH)
   {
    myServo.write(0);
    delay(2000);
    myServo.write(90);
   }
   
   delay(15); //waits for the servo
  }

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...