Introduction: Writing the Code

Picture of Writing the Code

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Setting Up the Circuit

Lesson Overview:

Now we'll write our simple software!

Step 1: Introduction

In this lesson, you will learn how to control several aspects of motor behavior using the H-bridge IC: turning the motor on and off, changing its speed with PWM, and changing its direction.

Our version of the Zoetrope circuit is in the Workplane. It should look similar to the one that build in the previous lesson!

  1. Continue to the next step.

Step 2: Name Your Constants

Picture of Name Your Constants

Start out the program by creating constants for the output and input pins.

  1. Copy the code below into the Code Editor, including comments. //H-bridge control and enable const int controlPin1 = 2; const int controlPin2 = 3; const int enablePin = 9; //button pins const int directionSwitchStatePin = 4; const int onOffSwitchStatePin = 5; //potentiometer pin const int potPin = A0;

  2. Continue to the next step.

Step 3: Create Button Variables

Picture of Create Button Variables

Next create variables to hold the values from your inputs. You’ll be doing state change detection for both buttons (i.e. switches), comparing the state from one loop to the next.

This is similar to the technique used in Digital Hourglass. So in addition to storing the current state, you’ll need to record the previous state of each switch.

  1. Copy the code below into the Code Editor. int onOffSwitchState = 0; int previousOnOffSwitchState = 0; int directionSwitchState = 0; int previousDirectionSwitchState = 0;

  2. Continue to the next step.

Step 4: Create Motor Control Variables

Picture of Create Motor Control Variables

The variable motorEnabled keeps track of whether the motor is actually on, motorSpeed monitors the rate of rotation, and motorDirection keeps track of which direction the motor is spinning.

  1. Copy the code below into the Code Editor. int motorEnabled = 0; int motorSpeed = 0; int motorDirection = 1;

  2. Continue to the next step.

Step 5: Declare Input and Output Pins

Picture of Declare Input and Output Pins

In the setup() function, set the direction of each input and output pin. You will also set the initial state of the enablePin to LOW.

Notice that this is the entire setup() function!

  1. Copy the code below into the Code Editor. void setup(){ pinMode(directionSwitchStatePin, INPUT); pinMode(onOffSwitchStatePin, INPUT); pinMode(controlPin1, OUTPUT); pinMode(controlPin2, OUTPUT); pinMode(enablePin, OUTPUT); //turn the motor off, initially digitalWrite(enablePin, LOW); }

  2. Continue to the next step.

Step 6: Read Sensor Information

Picture of Read Sensor Information

In your loop() function, read the state of both buttons and the potentiometer, and store the values in their respective variables.

  1. Copy the code below into the Code Editor. void loop(){ onOffSwitchState = digitalRead(onOffSwitchStatePin); delay(1); directionSwitchState = digitalRead(directionSwitchStatePin); motorSpeed = analogRead(potPin)/4;

  2. Continue to the next step.

Step 7: Check If On/off State Has Changed

Picture of Check If On/off State Has Changed

If there is a difference between the current switch state and the previous, and the switch state is currently HIGH then invert the motorEnabled variable using the inversion operator (!).

This block of code uses nested if() statements.

  1. Copy the code below into the Code Editor. if(onOffSwitchState != previousOnOffSwitchState){ if(onOffSwitchState == HIGH){ motorEnabled = !motorEnabled; } //end of nested if() } //end of outer if()

  2. After copying the code, you will see that there are two if() statements nested together.

  3. Continue to the next step.

Step 8: Check for a Change in Direction

Picture of Check for a Change in Direction

Check to see if the direction switch is currently in a different position than it was previously. If it is different, change the motor direction variable.

There are only 2 ways for the motor to spin, so you’ll want to alternate the variable between two states. You will use the inversion operator (!) again.

  1. Copy the code below into the Code Editor. if(directionSwitchState != previousDirectionSwitchState){ if(directionSwitchState ==HIGH){ motorDirection = !motorDirection; } //end of nested if() } //end of outer if()

  2. After copying the code, you will see that there are two if() statements nested together.

  3. Continue to the next step.

Step 9: Spin the Motor

Picture of Spin the Motor

The motorDirection variable determines which direction the motor is turning. To set the direction, you set the control pins with one HIGH and the other LOW.

In the following code, when the variable motorDirection changes you'll reverse the states of the control pins.

  1. Copy the code below into the Code Editor. if(motorDirection == 1){ digitalWrite(controlPin1, HIGH); digitalWrite(controlPin2, LOW); } else{ digitalWrite(controlPin1, LOW); digitalWrite(controlPin2, HIGH); }

  2. Continue to the next step.

Step 10: Set Motor Speed With PWM

Picture of Set Motor Speed With PWM

The following section determines whether the motor is already on, then sets its speed.

If the motorEnabled variable is 1, set the speed of the motor using analogWrite() to turn on the enable pin and set speed with PWM.

If motorEnabled is 0, then turn the motor off by setting the analogWrite value to 0.

  1. Copy the code below into the Code Editor. if (motorEnabled == 1){ //set motor speed with PWM analogWrite(enablePin, motorSpeed); } else{ analogWrite(enablePin, 0); }

  2. Almost there! Continue to the next step.

Step 11: Save Current States for the Next Loop.

Picture of Save Current States for the Next Loop.

Before exiting the loop() function, save the current state of the switches as the previous state for the next run through the program.

This is the last step in the program!

  1. Copy the code below into the Code Editor. previousDirectionSwitchState = directionSwitchState; previousOnOffSwitchState = onOffSwitchState; } // end of loop()

  2. Continue to the next step.

Step 12: Use It!

Picture of Use It!

Upload and run your code on the circuit simulator. If you are using your Arduino kit, plug in your Arduino and attach the 9V battery before uploading and running the code.

In the picture below, we have already added a wooden adapter to the motor spindle - you will do this in the next lesson!

  1. Upload & Run your code in the simulator, or plug in your battery and upload the code to your Arduino Uno.

  2. When you press the On/Off button, the motor will start spinning! Pressing again will turn it off.

  3. If you turn the potentiometer, the motor should speed up and slow down.

  4. Try pressing the direction button and verify that the motor spins both ways.

  5. Continue to the next step.

Step 13: Review

Congratuations, you have used an integrated circuit to control several aspects of motor behavior! The H-bridge can turn the motor on and off, changing its speed, and change its direction.

In the next lesson, you will learn more about how the zoetrope mechnaism works, as you put together the tangible part of the project.

Next Lesson:Making the Zoetrope

Comments

About This Instructable

1,035views

6favorites

License:

Bio: Circuit lessons for Tinkercad Circuits!
More by circuits:IntroductionProject 15: Hacking ButtonsProject 14: Tweak the Arduino Logo
Add instructable to: