Samba Hearts - Create (Heart) Beats With Arduino (code Only)

Introduction: Samba Hearts - Create (Heart) Beats With Arduino (code Only)

About: My name is Sabina Stan. I am a visual artist based in Romania. I work with rope, paper and recycled aluminium cans and I can do just about anything (figurative or abstract) in the technique I use, which is an …

For my latest project called Heartbeats, I did a series of hearts made of aluminium cans that beat and because I figured that a simple, calm heart beat is not enough to highlight the richness of feelings that reside in a human heart, I chose to use the Samba intro that I learnt this year at the percussion classes I took.

I decided to share here the code I used for the different versions. I already made a tutorial about the how to create the hearts and there is no shortage of tutorials about how to wire the different components to an Arduino.

Supplies

  • Arduino
  • Servo motors + wire to fix the motor in the heart
  • Sensor or buttons - optional

Step 1: Simple Heartbeat

In order to create a simple heartbeat I would need for the servo to move back, wait a very short moment, move forth and wait a "little" longer depending on how calm I want the heart beat to be and then repeat.

#include <Servo.h>
Servo servo1;          

void setup() { 
     servo1.attach(10);
} 

void loop() {
     servo1.write(110);
     delay (150);
     servo1.write(10);
     delay (1000);
}

The position of the servo is chosen depending on (whichever is smallest):

- how much I want to make the heart contract

- how much the servo is allowed to move by the way it is positioned

- how much the servo can move by design (some are 360 degrees, others 180, others 120)

Step 2: Music Notes Turned to Code

In order to create drum beats, I play with the pauses between the movement of the servo only. The positions back an forth remain the same, namely 110 and 10 in my case.

Samba is based on a four stroke rhythm. So, what I did was to decide how long the whole note should last. I chose delay (600) for the full note, which made a half note 300 and a quarter note 150, which would be the main note in a samba rhythm, and so on.

For the notes that were suppose to be accents, I increased whatever delay they had with 50.

You would only change the position of the servo, if you want to change the feel from piano to forte. I haven't explored this part much.

.

Step 3: Heart With IR Sensor and Two Heartbeats

I made a heart that reacts to closeness and that does not beat unless you get in it's range and when you get too close it beats faster.

#include <Servo.h>
#include <SharpIR.h>

Servo servo1;

#define IRPin A0
#define model 430

int distance;

SharpIR mySensor = SharpIR(IRPin, model);

void setup() {
servo1.attach(10);
Serial.begin(9600);
}

void loop() {
distance = mySensor.distance();
Serial.println(distance);
if (distance >= 0 && distance <= 5) {
delay (1000);
servo1.write(110);
delay (450);
servo1.write(10);
delay (300);
servo1.write(110);
delay (300);
servo1.write(10);
delay (450);
servo1.write(110);
delay (300);
servo1.write(10);
delay (300);
servo1.write(110);
delay (450);
servo1.write(10);
delay (300);
servo1.write(110);
delay (300);
servo1.write(10);
delay (300);
servo1.write(110);
delay (450);
servo1.write(10);
delay (300);
servo1.write(110);
delay (300);
servo1.write(10);
delay (450);
servo1.write(110);
delay (300);
servo1.write(10);
delay (300);
}
if (distance >= 6 && distance <= 26) {
servo1.write(110);
delay (150);
servo1.write(10);
delay (1000);
}
else {
servo1.write(10);
}
}

Note: If the delay is 450 it means there is a half note with a pause as long as a quarter note until the next beat.

Step 4: Samba Hearts With Button

The samba intro has 6 rhythms, so I put them each in a function. Each rhythm repeats twice.

I made two hearts that sing together as only one is not nearly as engaging. I find the music to gain from the communication between drums rather than on drum starring.

#include <Servo.h>

Servo servo1;
Servo servo2;

const int buttonPin = 9;
int buttonState = LOW;

void setup() {
servo1.attach(10);
servo2.attach(11);
pinMode(buttonPin, INPUT);
Serial.begin (9600);

}

void loop() {
buttonState = digitalRead(buttonPin);

Serial.println (buttonState);

if (buttonState == HIGH) {
delay (2300);
for (int i=0; i<2; i++){
ritm1 ();
}
for (int i=0; i<2; i++){
ritm2 ();
}
for (int i=0; i<2; i++){
ritm3 ();
}
for (int i=0; i<2; i++){
ritm4 ();
}
for (int i=0; i<2; i++){
ritm5 ();
}
ritm6 ();
}
}

void ritm1 () {
servo1.write(110);
delay (200);
for (int j=0; j<2; j++){
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (350);
servo1.write(10);
delay (200);
}
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (200);
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (400);

servo2.write(110);
delay (200);
for (int j=0; j<2; j++){
servo2.write(10);
delay (150);
servo2.write(110);
delay (150);
servo2.write(10);
delay (200);
servo2.write(110);
delay (350);
servo2.write(10);
delay (200);
}
servo2.write(10);
delay (150);
servo2.write(110);
delay (150);
servo2.write(10);
delay (200);
servo2.write(110);
delay (150);
servo2.write(10);
delay (150);
servo2.write(110);
delay (200);
servo2.write(10);
delay (150);
servo2.write(110);
delay (150);
servo2.write(10);
delay (200);
servo2.write(110);
delay (400);

}

void ritm2 () {
servo1.write(110);
delay (250);
servo1.write(10);
delay (250);
servo1.write(110);
delay (250);
servo1.write(10);
delay (250);
servo1.write(110);
delay (300);
servo1.write(10);
delay (250);
servo1.write(110);
delay (250);
servo1.write(10);
delay (500);

servo2.write(10);
delay (250);
servo2.write(110);
delay (250);
servo2.write(10);
delay (550);
servo2.write(110);
delay (300);
servo2.write(10);
delay (250);
servo2.write(110);
delay (250);
servo2.write(10);
delay (500);
}

void ritm3 () {
for (int i=0; i<3; i++){
servo1.write(110);
delay (75);
servo1.write(10);
delay (75);
servo1.write(110);
delay (75);
servo1.write(10);
delay (125);
servo1.write(110);
delay (600);
}
servo2.write(110);
delay (600);
servo2.write(10);
delay (600);
}

void ritm4 () {
servo1.write(110);
delay (350);
servo1.write(10);
delay (200);
servo1.write(110);
delay (400);
servo1.write(10);
delay (200);
servo1.write(110);
delay (200);
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (600);

servo2.write(110);
delay (350);
servo2.write(10);
delay (200);
servo2.write(110);
delay (400);
servo2.write(10);
delay (200);
servo2.write(110);
delay (200);
servo2.write(10);
delay (150);
servo2.write(110);
delay (150);
servo2.write(10);
delay (150);
servo2.write(110);
delay (600);
}

void ritm5 () {
for (int j=0; j<2; j++){
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (600);
}
servo1.write(10);
delay (150);
servo1.write(110);
delay (200);
servo1.write(10);
delay (75);
servo1.write(110);
delay (150);
servo1.write(10);
delay (75);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (600);

for (int j=0; j<2; j++){
servo2.write(110);
delay (150);
servo2.write(10);
delay (200);
servo2.write(110);
delay (600);
}
servo2.write(10);
delay (150);
servo2.write(110);
delay (200);
servo2.write(10);
delay (75);
servo2.write(110);
delay (150);
servo2.write(10);
delay (75);
servo2.write(110);
delay (150);
servo2.write(10);
delay (200);
servo2.write(110);
delay (600);
}

void ritm6 () {
for (int i=0; i<2; i++){
servo1.write(110);
delay (75);
servo1.write(10);
delay (150);
servo1.write(110);
delay (175);
servo1.write(10);
delay (175);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo2.write(10);
delay (200);
servo2.write(110);
delay (600);
}
servo1.write(110);
delay (75);
servo1.write(10);
delay (150);
servo1.write(110);
delay (75);
servo1.write(10);
delay (150);
servo2.write(10);
delay (200);
servo1.write(10);
delay (75);
servo1.write(110);
delay (150);
servo1.write(10);
delay (75);
servo1.write(110);
delay (150);
servo2.write(110);
delay (200);
for (int i=0; i<2; i++){
servo1.write(110);
delay (150);
servo2.write(10);
delay (150);
servo1.write(10);
delay (150);
servo2.write(110);
delay (200);
}
}

Step 5: Rainbow Heart With Four States

I figured that a colourful heart should have a richness of states, so I user a joystick button to make it react differently.

For these example I use some of the samba intro rhythms and just like for the last code, each rhythm is in its own function, but now there is only one heart playing everything.

#include <Servo.h>

Servo servo1;

int VRx = A0;
int VRy = A1;
int SW = 2;

int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;

void setup() {
servo1.attach(10);
Serial.begin(9600);

pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);

}

void loop() {
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
SW_state = digitalRead(SW);
mapX = map(xPosition, 0, 1023, -512, 512);
mapY = map(yPosition, 0, 1023, -512, 512);

Serial.print("X: ");
Serial.print(mapX);
Serial.print(" | Y: ");
Serial.print(mapY);
Serial.print(" | Button: ");
Serial.println(SW_state);

delay(100);

if (mapX > 300) {
ritm1 ();
}
if (mapX < -300) {
ritm2 ();
}
if (mapY > 300) {
ritm3 ();
}
if (mapY < -300) {
ritm4 ();
}
}
void ritm1 () {
servo1.write(180);
delay (200);
for (int j=0; j<2; j++){
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (350);
servo1.write(10);
delay (200);
}
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (200);
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (200);
servo1.write(10);
delay (400);

servo1.write(110);
delay (200);
for (int j=0; j<2; j++){
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (350);
servo1.write(10);
delay (200);
}
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (200);
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (200);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (200);
servo1.write(10);
delay (400);

}

void ritm2 () {
servo1.write(110);
delay (250);
servo1.write(10);
delay (250);
servo1.write(110);
delay (250);
servo1.write(10);
delay (250);
servo1.write(110);
delay (300);
servo1.write(10);
delay (250);
servo1.write(110);
delay (250);
servo1.write(10);
delay (500);

servo1.write(110);
delay (250);
servo1.write(10);
delay (250);
servo1.write(110);
delay (550);
servo1.write(10);
delay (300);
servo1.write(110);
delay (250);
servo1.write(10);
delay (250);
servo1.write(110);
delay (500);
}

void ritm3 () {
for (int i=0; i<3; i++){
servo1.write(10);
delay (75);
servo1.write(110);
delay (75);
servo1.write(10);
delay (75);
servo1.write(110);
delay (125);
servo1.write(10);
delay (600);
}
servo1.write(110);
delay (600);
servo1.write(10);
delay (600);
}

void ritm4 () {
servo1.write(110);
delay (350);
servo1.write(10);
delay (200);
servo1.write(110);
delay (400);
servo1.write(10);
delay (200);
servo1.write(110);
delay (200);
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (600);

servo1.write(10);
delay (350);
servo1.write(110);
delay (200);
servo1.write(10);
delay (400);
servo1.write(110);
delay (200);
servo1.write(10);
delay (200);
servo1.write(110);
delay (150);
servo1.write(10);
delay (150);
servo1.write(110);
delay (150);
servo1.write(10);
delay (600);
}

Step 6: Corset With Beating Heart That Reacts to Movement

Following the Heartbeats series, I made a wearable corset out of aluminium cans which has a heart popping out of the chest and that beats when some of is moving close enough.

This heart beats steady, but only when there is movement around, namely only on the side of the heart where the pir sensor is placed.

#include <Servo.h>
int sensor1 = 14;
Servo servo1;
int val1 = LOW;

void setup() {
servo1.attach(10);
pinMode(sensor1, INPUT);

}

void loop() {
val1 = digitalRead(sensor1);
if (val1 == HIGH) {
servo1.write(110);
delay (150);
servo1.write(10);
delay (1000);
}
}

Be the First to Share

    Recommendations

    • Tiny Things Speed Challenge

      Tiny Things Speed Challenge
    • Paint Challenge

      Paint Challenge
    • Made with Math Contest

      Made with Math Contest

    Comments