My Cart

How to Play Melodies and Videos Using Processing and Waspmote Starter Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to play a simple melody with a buzzer pushing some buttons and also play a video with Processing software.

Ingredients:

  • 1 x Waspmote Starter Kit:
    • 1x Waspmote 802.15.4 uFL
    • 1x Waspmote Gateway 802.15.4 uFL
    • 1x 2300 mAh LiPo Battery
    • 1x miniUSB Cable
  • 1 x Starter Kit:
    • 4x Buttons
    • 4x 220Ω resistors
    • 1x Buzzer
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now

For further information about Waspmote, consult the main tutorial.

Step 1: Connection

Connect the buttons with the resistors and the buzzer in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to 3.3V in and ground.

Connect digital pin 2, 4, 6 and 7 to one leg of each pushbutton. That same leg of the button connects through a pull-down resistor to ground. The other leg of the button connects to the 3.3V.

Connect digital pin 1 to the positive terminal of the buzzer and the other leg to GND.

Finally connect the XBee in the correct socket as you can see in the diagram and plug the XBee gateway in your PC.

Processing program is needed to run the full example. You can download the free software and the videos in the links below.

Unzip data folder and place it in your main sketch folder.

Download Processing here.

Download videos here.

Step 2: The Code

Waspmote:

Code:
/*  
 *  Waspmote Starter Kit
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Luis Miguel Marti, Marcos Martinez & Victor Boria 
 */


#include <WaspXBee802.h>


////////////////////////////////////////////////
// XBee Parameters
////////////////////////////////////////////////

// Destination MAC address
char* MAC_ADDRESS="0013A200400A3451";

packetXBee* packet; 
char pac[40];


void setup() {

  ////////////////////////////////////////////////
  // 0. Initialization
  ////////////////////////////////////////////////
  
  // 0.1 USB port for debugging
  USB.ON();
  delay(100);
  
  ////////////////////////////////////////////////
  // 2. Pin Configuration
  ////////////////////////////////////////////////
  
  pinMode(DIGITAL1,OUTPUT);
  pinMode(DIGITAL2,INPUT);
  pinMode(DIGITAL4,INPUT);
  pinMode(DIGITAL6,INPUT);
  pinMode(DIGITAL8,INPUT);

  // Turn on sensor power
  PWR.setSensorPower(SENS_3V3,SENS_ON);
  
  // Turn on XBee
  xbee802.ON();
}


void loop() {

  ////////////////////////////////////////////////
  // 3. Check button pressed
  ////////////////////////////////////////////////

  if(digitalRead(DIGITAL2)){
	// USB debugging
    USB.println(F("1"));
    
    // Sending packet via XBee
    snprintf(pac,sizeof(pac),"1\n");
    sendXbee(pac);
    
    //Enter while button is pressed
    while (digitalRead(DIGITAL2)){
      // Play buzzer tone
      tone(1136);
    }
  }
  
  if(digitalRead(DIGITAL4)){
    // USB debugging
    USB.println(F("2"));
    
    // Sending packet via XBee
    snprintf(pac,sizeof(pac),"2\n");
    sendXbee(pac);
    
    //Enter while button is pressed
    while (digitalRead(DIGITAL4)){
      // Play buzzer tone
      tone(1275);
    }
  }
  
  if(digitalRead(DIGITAL6)){
    // USB debugging
    USB.println(F("3"));
    
    // Sending packet via XBee
    snprintf(pac,sizeof(pac),"3\n");
    sendXbee(pac);
    
    //Enter while button is pressed
    while (digitalRead(DIGITAL6)){
      // Play buzzer tone
      tone(1431);
    }
  }
  
  if(digitalRead(DIGITAL8)){
    // USB debugging
    USB.println(F("4"));
    
    // Sending packet via XBee
    snprintf(pac,sizeof(pac),"4\n");
    sendXbee(pac);

    //Enter while button is pressed
    while (digitalRead(DIGITAL8)){
      // Play buzzer tone
      tone(1517);
    }
  }
}


////////////////////////////////////////////////
// Play tone function
////////////////////////////////////////////////

void tone(int time){
  digitalWrite(DIGITAL1,HIGH);
  delayMicroseconds(time);
  digitalWrite(DIGITAL1,LOW);
  delayMicroseconds(time);
}


////////////////////////////////////////////////
// Send XBee function
////////////////////////////////////////////////

void sendXbee(char* str){
	
  // Memory allocation
  packet=(packetXBee*) calloc(1,sizeof(packetXBee));
  
  // Choose transmission mode: UNICAST or BROADCAST
  packet->mode=BROADCAST; 
  
  // set destination XBee parameters to packet
  xbee802.setDestinationParams( packet, MAC_ADDRESS,str, MAC_TYPE);
  
  // Turn off XBee
  xbee802.ON();
  delay(100);
  
  // send XBee packet
  xbee802.sendXBee(packet);
  
  // free variables
  free(packet);
  packet=NULL;
  
  // Turn off XBee
  xbee802.OFF();
  
}

         

Processing code:

Code:
import processing.video.*;
import processing.serial.*;

Movie mov1;
Movie mov2;
Movie mov3;
Movie mov4;
Movie mov5;
int time=0;
boolean a = true;
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
char[] recibido = new char[10];


void setup() 
{
  size(720, 480);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, Serial.list()[32], 115200);  
  mov1 = new Movie(this, "marcos.mp4");
  mov2 = new Movie(this, "victor.mp4");
  mov3 = new Movie(this, "luis.mp4");
  mov4 = new Movie(this, "mi.mp4");
  mov5 = new Movie(this, "quietos.mp4");
  myPort.clear();
  mov1.loop();
  mov2.loop();
  mov3.loop();
  mov4.loop();
  mov5.loop();

}

void movieEvent(Movie movie) {
  movie.read();
  image(movie,0,0);
}

void draw()
{
  if ( myPort.available() > 0) {     // If data is available,
    recibido[0] = myPort.readChar(); // read it and store it in val
    a=true;
  }
    
    if (recibido[0]=='1'&&a){
      
        mov1.play();
        mov2.pause();
        mov3.pause();
        mov4.pause();
        mov5.pause();
        fill(255);
        time = millis();
        a=true;
    }
    
    if (recibido[0]=='2'&&a){
        mov2.play();
        mov1.pause();
        mov3.pause();
        mov4.pause();
        mov5.pause();
        fill(255);
        time = millis();
        a=true;
    }
    
    if (recibido[0]=='3'&&a){
        mov3.play();
        mov1.pause();
        mov2.pause();
        mov4.pause();
        mov5.pause();
        fill(255);
        time = millis();
        a=true;
    }
    
    if (recibido[0]=='4'&&a){
        mov4.play();
        mov1.pause();
        mov2.pause();
        mov3.pause();
        mov5.pause();
        fill(255);
        time = millis();
        a=true;
    }
    if(millis()-time>1500){
        a = false;
        mov1.pause();
        mov2.pause();
        mov3.pause();
        mov4.pause();
        mov5.play();
    }

}