obniz developer team
Published

Air Drumming Into Real Drum

If you play air drum on the table, real drum will be played physically following your hand position.

BeginnerFull instructions provided533
Air Drumming Into Real Drum

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
for distance sensor and 2 servo motors
×3
power supply for obniz
×3
servo motor
×2
Proximity Sensor
Proximity Sensor
GP2Y0A21YK0F(IR distance sensor)
×2
mini drum and sticks
×1

Story

Read more

Code

Untitled file

JavaScript
const Obniz = require("obniz");

const disObniz = new Obniz("OBNIZ_ID_FOR_DISTANCE_HERE");
const drumObnizes = [new Obniz("OBNIZ_ID_FOR_DRUM_1_HERE"), new Obniz("OBNIZ_ID_FOR_DRUM_2_HERE")];

//DEFAULT_SERVO_ANGLE is the angle of the servo motor
//please adjust this value properly
const DEFAULT_SERVO_ANGLE = 40.0;
const BACK_SERVO_PARAM = {default: 40.0, leftdrum: 125.0, cymbal: 12.0};
const FRONT_SERVO_PARAM = {default: 40.0, centerdrum: 88.0, rightdrum: 25.0};

disObniz.onconnect = async () => {
	
	let disLeft = disObniz.wired("GP2Y0A21YK0F", {vcc:0, gnd:1, signal:2});
	let disRight = disObniz.wired("GP2Y0A21YK0F", {vcc:6, gnd:7, signal:8});
	let servoMotors = [];
	
	drumObnizes.forEach(async (obniz, index) => {
		
		if(drumObnizes[index].connectionState === 'connected'){
			
			await console.log(drumObnizes[index].connectionState);
			let servo = obniz.wired("ServoMotor", {signal:9,vcc:10, gnd:11});
			servoMotors[index] = servo;
			
		}else{
			
			await drumObnizes[index].wait(500);
			let servo = obniz.wired("ServoMotor", {signal:9,vcc:10, gnd:11});
			servoMotors[index] = servo;
			await console.log(drumObnizes[index].connectionState);
			
		}
		
		await console.log(servoMotors.length + "motors");
	});
	
	const rotateServo = async (area, servoAngle) => {
		
		if(servoMotors[area] === undefined || servoMotors[area] === null){
			return;
		}

		await servoMotors[area].angle(servoAngle);
		await resetServo(area);
	}
	
	const resetServo = async area => {
		servoMotors.forEach(async (servo, index) => {
			if(index === area || drumObnizes[index].connectionState !== 'connected'){
				return;
			}
			await servo.angle(DEFAULT_SERVO_ANGLE);
		});
	}
	
	//for the first loop
	let prevArea = {left: 2, right: 2};
	let nowArea = {left: 3, right: 3};
	
	//area division(every 140mm)
	//left0 => upper left: left drum
	//right0 => upper right: cymbal
	//left1 => lower left: center drum
	//right1 => lower right: right drum
	const AREA_THRESHOLD = 140;
	
	while(true){

		
		let leftPos = await Math.floor(await disLeft.getWait() / AREA_THRESHOLD);
		let rightPos = await Math.floor(await disRight.getWait() / AREA_THRESHOLD);
		
		nowArea.left = leftPos;
		nowArea.right = rightPos;
		
		if(prevArea.left === nowArea.left && prevArea.right === nowArea.right){
			continue;
		}else if(prevArea.left > 1 && nowArea.left > 1 && prevArea.right > 1 && nowArea.right > 1){
			continue;
		}
		
		await console.log(nowArea);
		
		
		//area division
		if(nowArea.left > 1 || nowArea.right > 1){
			await console.log("out of area");
			await resetServo(nowArea);
		}
		if(nowArea.left === 0){
			await console.log("left drum");
			await rotateServo(nowArea.left, BACK_SERVO_PARAM.leftdrum);
			
		}else if(nowArea.left === 1){
			await console.log("center drum");
			await rotateServo(nowArea.left, FRONT_SERVO_PARAM.centerdrum);
			
		}
		if(nowArea.right === 0){
			await console.log("cymbal");
			await rotateServo(nowArea.right, BACK_SERVO_PARAM.cymbal);
			
		}else if(nowArea.right===1){
			await console.log("right drum");
			await rotateServo(nowArea.right, FRONT_SERVO_PARAM.rightdrum);
			
		}
		
		prevArea.left = nowArea.left;
		prevArea.right = nowArea.right;
		
	}

	
}

Credits

obniz developer team

obniz developer team

80 projects • 35 followers
Development board "obniz" is controlled via the internet.

Comments