We use the Mouse object to draw a set of squares in Microsoft Paint. It should also work in other drawing programs. Upload this program, open Paint, and place the mouser cursor near the top-left part of the canvas. Press SW1 to start drawing the squares. Press SW2 to stop.

We use five int variables to draw the squares. The first two determine the direction to move the mouse: int stepX = 0; int stepY = 0; The next two keep track of how far the mouse has moved on each side of the squares: int toX = 0; int toY = 0; And the fifth variable keeps track of which side we’re working on: int side = 1; We start the loop() block by checking to see if SW1 is pressed. If it is, we create an instance of the Mouse object. Next, we use the Mouse.press command to press the left mouse button down and hold it. This allows us to draw a line in Paint: if(digitalRead(SW1)==LOW){ Mouse.begin(); Mouse.press(); We next enter a while loop that will continue to draw squares until SW2 is pressed: while(digitalRead(SW2)==HIGH){ The next set of statements setup the variables stepX and stepY to control the mouse movement depending on which side of the square we’re drawing. Side 1 draws a horizontal line to the right: if(side == 1){ stepX = 1; stepY = 0; } Side 2 draws a vertical line upwards: if(side == 2){ stepX = 0; stepY = -1; } Side 3 draws a horizontal line back to the left: if(side == 3){ stepX = -1; stepY = 0; } Side 4 completes the square by moving vertically downwards: if(side == 4){ stepX = 0; stepY = 1; } Next, we move the mouse according to stepX and stepY: Mouse.move(stepX,stepY,0); We move one pixel at a time. We keep track of how far we’ve moved using toX and toY. The abs function takes the absolute (positive) value of the step variables so we can keep track of how many pixels we’ve moved even if we’re moving backwards: toX = toX + abs(stepX); toY = toY + abs(stepY); Once we’ve moved 100 pixels, we switch to the next side and reset toX and toY: if(toX > 100 || toY > 100){ side = side + 1; toX = 0; toY = 0; It won’t be very interesting if we just keep retracing the same square. We make side 3 and side 2 shorter than the other two sides by setting toX and toY to 10 from the start: if(side == 3) toX = 10; if(side == 2) toY = 10; If side reaches 5 then we reset it to 1: if(side == 5) { side = 1; } Once we break out of the if loop for SW1, we release the mouse button: } //end if-then for SW1 Mouse.release();