Weeks 4-6: Piezo Buzzer and Servo Motor w/ Sensors

Octi
Reactive Octi
Based on the pressure or flex applied to the embedded sensors in Octi’s arms, his eyes rotate to make him look either sad or mad.
#include <Servo.h>
Servo myservo;
int servo = 7;
int piezo = 8;
int pressure = A0;
int servo_pos = 0;
int current_pressure = 0;
int current_note = 200;
int current_spin = 1;
void setup() {
  // put your setup code here, to run once:
  pinMode(servo, OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(pressure, INPUT);
  myservo.attach(servo);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  current_pressure = analogRead(pressure);
  Serial.print(current_pressure);
  Serial.print(” “);
  current_note = map(current_pressure, 0, 1024, 200, 2000);
  Serial.print(current_note);
  Serial.print(” “);
  current_spin = map(current_pressure, 0, 1024, 1, 20);
  Serial.println(current_spin);
  tone(piezo, current_note);
  myservo.write(servo_pos);
  servo_pos += 1*current_spin;
}

Leave a Reply

Your email address will not be published. Required fields are marked *