Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 7.04 Draw Squares 

 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.


Concepts: Mouse object

Circuits:
  • Circuit 2


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 7.04 Draw Boxes
    int SW1 = 1;
    int SW2 = 4;
    int toX = 0;
    int toY = 0;
    int stepX = 0;
    int stepY = 0;
    int side = 1;
    void setup(){
    pinMode(SW1,INPUT);
    pinMode(SW2,INPUT);
    }
    void loop(){
    if(digitalRead(SW1)==LOW){
    Mouse.begin();
    Mouse.press();
    while(digitalRead(SW2)==HIGH){
    if(side == 1){
    stepX = 1;
    stepY = 0;
    }
    if(side == 2){
    stepX = 0;
    stepY = -1;
    }
    if(side == 3){
    stepX = -1;
    stepY = 0;
    }
    if(side == 4){
    stepX = 0;
    stepY = 1;
    }
    Mouse.move(stepX,stepY,0);
    toX = toX + abs(stepX);
    toY = toY + abs(stepY);
    if(toX > 100 || toY > 100){
    side = side + 1;
    toX = 0;
    toY = 0;
    if(side == 3) toX = 10;
    if(side == 2) toY = 10;
    if(side == 5) {
    side = 1;
    } // end move back to side 1
    } //end change side
    delay(5);
    } //end while for SW2
    } //end if-then for SW1
    Mouse.release();
    } //end loop()
    ///////////////////////////////////////////////////

    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();

    Back to Projects 7

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.