# Let’s develop a desk cleaning robot!

Recently, robots that clean the floor are often seen, such as rumba. However, I thought that I didn’t see a robot that cleans desk, so I decided to make one.

## Index

1.Description of tabletop cleaning robot

First of all, in order to make this robot, it is necessary to avoid obstacle and not fall from the desk. For the purpose, let the infrared sensor detect obstacles and the edge of the desk. The mechanism is explained in the figure below.

The robot avoids when Infrared sensor detects7cm or less or 20cm or more.

Next, I would like to explain the running program in the figure below

With such a program, the robot avoids obstacle and rans.

2. Improvement

This time, I would like to improve the following points.

・Make it possible to finish running at the designate time.

・Play a sound to signal the end.

・Make the body as compact as possible.

3.Ring on voltage speaker

① Use a piezoelectric speaker

I used a piezo speaker to make a sound to signal the end. By repeating the output of piezoelectric speaker on and off quickly, it is possible to create a sound by vibrating the air. The frequency can be adjusted by changing the on/off cycle, since “frequency = 1/cycle”.

② Frequency calculation

Consider the waiting time x to generate the frequency. ( x is in microseconds.)
1cycle=x × 0.000001 × 2
Frequency = 1/cycle
Frequency = 1/( x × 0.000001 × 2 )
x = 1/( Frequency × 0.000001 × 2)
If you want to make a sound with a frequency of 300 Hz, x=1/(300 × 0.000001 × 2) ≒ 1666.67[us]
Set waiting time to 1666.67.

③ play music

I thought that it would be possible to make music by adjusting
the frequency of sound, so I investigated the scale of frequency.
I decided to try “Dreams and Awakening of Dolls”.
As a result, I was able to smooth out the melody, which is not accurate, but seems to be “doll’s dream and awakening”.

4.Timer function

Using a library called Timer1 for Arduino, I created a program that plays the melody I made last time after 10 seconds.

5.Changed the shape of the crawler

The cloth on the back of the car was a bit of an obstacle, so I put it under the car and widened the car for that purpose.  And I painted it to improve the appearance.

①Painting

The tire were painted silver and the other were painted black using spray.

②Assemble

The size itself is a little bigger, but I was able to put a cleaning cloth under the car body.

6.Operation experiment

After confirming the operation, when I pressed the button, It was able to run properly, avoiding the edges of the desk and obstacles. Then It stopped at the set time and could play the music.

7. program

The following program is running this robot.

#include <TimerOne.h>

#define LEFT_M 13

#define LEFT_S 11

#define RIGHT_M 12

#define RIGHT_S 3

#define FORWARD HIGH

#define REVERSE LOW

#define ST_SWITCH 7

#define LED_PIN  6

#define DET_LED 5

#define LED 4

volatile int count_time;

void count_one_sec()

{

count_time–;

digitalWrite(LED,HIGH);

delay(50);

digitalWrite(LED,LOW);

}

void full_stop()

{

analogWrite(LEFT_S, 0);

analogWrite(RIGHT_S, 0);

}

void forward(int mspeed,int mtime)

{

digitalWrite(LEFT_M, FORWARD);

analogWrite(LEFT_S, mspeed);

digitalWrite(RIGHT_M, FORWARD);

analogWrite(RIGHT_S, mspeed);

if(mtime > 0){

delay(mtime);

analogWrite(LEFT_S, 0);

analogWrite(RIGHT_S, 0);

}

}

void reverse(int mspeed,int mtime){

digitalWrite(LEFT_M, REVERSE);

analogWrite(LEFT_S, mspeed);

digitalWrite(RIGHT_M, REVERSE);

analogWrite(RIGHT_S, mspeed);

if(mtime > 0){

delay(mtime);

analogWrite(LEFT_S, 0);

analogWrite(RIGHT_S, 0);

}

}

void left(int mspeed,int mtime){

digitalWrite(LEFT_M, REVERSE);

analogWrite(LEFT_S, mspeed);

digitalWrite(RIGHT_M, FORWARD);

analogWrite(RIGHT_S, mspeed);

if(mtime > 0){

delay(mtime);

analogWrite(LEFT_S, 0);

analogWrite(RIGHT_S, 0);

}

}

void right(int mspeed,int mtime){

digitalWrite(LEFT_M, FORWARD);

analogWrite(LEFT_S, mspeed);

digitalWrite(RIGHT_M, REVERSE);

analogWrite(RIGHT_S, mspeed);

if(mtime > 0){

delay(mtime);

analogWrite(LEFT_S, 0);

analogWrite(RIGHT_S, 0);

}

}

int front_sens()

{

int tmp,distance;

if(tmp < 4) tmp=4;

distance=(6787 / (tmp – 3)) -4;

return(distance);

}

void setup()

{

pinMode(LEFT_M, OUTPUT);

pinMode(RIGHT_M, OUTPUT);

pinMode(LEFT_S, OUTPUT);

pinMode(RIGHT_S, OUTPUT);

pinMode(LED_PIN, OUTPUT);

pinMode(DET_LED, OUTPUT);

pinMode(ST_SWITCH, INPUT);

pinMode(9,OUTPUT);

pinMode(LED,OUTPUT);

Serial.begin(9600);

count_time=0;

}

void loop()

{

int left_val;

int right_val;

int i;

count_time=0;

full_stop();

while(1)

{

}

digitalWrite(LED_PIN, HIGH);

delay(3000);

count_time=300;

Timer1.initialize(1000000L);

Timer1.attachInterrupt(count_one_sec);

while(1)

{

if(front_sens() > 9 && front_sens()<20) forward(100,1000);

if(front_sens() >= 20 || front_sens()<=9)

{

digitalWrite(DET_LED,HIGH);

full_stop();

delay(500);

reverse(100,800);

left_val=0;

right_val=0;

for(i=0;i<3;i++)

{

left(120,300);

if(front_sens() < 20 && front_sens()>9) left_val++;

}

for(i=0;i<3;i++)

{

right(120,300);

}

for(i=0;i<3;i++)

{

right(120,300);

if(front_sens()<20 && front_sens()>9) right_val++;

}

for(i=0;i<3;i++)

{

left(120,300);

}

if(left_val>right_val)

{

while(1)

{

left(120,1000);

if(front_sens()<20 && front_sens()>9) break;

}

}

else if(right_val<left_val)

{

right(100,1000);

if(front_sens()<20 && front_sens()>9) break;

}

else

{

if(right_val==0)

{

reverse(100,1000);

right(100,2500);

}

else if(right_val==3)

{

reverse(100,800);

left(100,2500);

}

else

{

reverse(100,1000);

left(100,2500);

}

}

digitalWrite(DET_LED,LOW);

}

if(count_time<=0)

{

full_stop();

Timer1.detachInterrupt();

break;

}

}

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(318.89);

digitalWrite(9,LOW);

delayMicroseconds(318.89);

}

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(357.88);

digitalWrite(9,LOW);

delayMicroseconds(357.88);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(379.19);

digitalWrite(9,LOW);

delayMicroseconds(379.19);

}

delay(50);

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(318.89);

digitalWrite(9,LOW);

delayMicroseconds(318.89);

}

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(238.89);

digitalWrite(9,LOW);

delayMicroseconds(238.89);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(253.1);

digitalWrite(9,LOW);

delayMicroseconds(253.1);

}

delay(50);

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(318.89);

digitalWrite(9,LOW);

delayMicroseconds(318.89);

}

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(212.79);

digitalWrite(9,LOW);

delayMicroseconds(212.79);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(238.89);

digitalWrite(9,LOW);

delayMicroseconds(238.89);

}

for(i=0;i<1000;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(189.6);

digitalWrite(9,LOW);

delayMicroseconds(189.6);

}

delay(50);

for(i=0;i<1000;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(189.6);

digitalWrite(9,LOW);

delayMicroseconds(189.6);

}

delay(50);

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(238.89);

digitalWrite(9,LOW);

delayMicroseconds(238.89);

}

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(253.1);

digitalWrite(9,LOW);

delayMicroseconds(253.1);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(284.09);

digitalWrite(9,LOW);

delayMicroseconds(284.09);

}

delay(50);

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(357.88);

digitalWrite(9,LOW);

delayMicroseconds(357.88);

}

for(i=0;i<400;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(212.79);

digitalWrite(9,LOW);

delayMicroseconds(212.79);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(238.89);

digitalWrite(9,LOW);

delayMicroseconds(238.89);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(253.1);

digitalWrite(9,LOW);

delayMicroseconds(253.1);

}

for(i=0;i<800;i++)

{

digitalWrite(9,HIGH);

delayMicroseconds(238.89);

digitalWrite(9,LOW);

delayMicroseconds(238.89);

}

delay(1000);

}

<References>

Arduinoで電子工作をはじめよう！第２版