Created
January 3, 2020 04:49
-
-
Save Levi-Lesches/a532c763f5263eac2cb9ac5373197bc7 to your computer and use it in GitHub Desktop.
A sketch to control an H-Bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These "#define" statements are *like* variables, but not really | |
// Variables can be changed to any other value, but these cannot | |
// They don't even stay in memory! What happens is, when you press compile, | |
// the Arduino replaces all the times the name comes up in the code with the | |
// value before sending it to the Arduino board. So it's not really a variable | |
// you can control, but rather just another name for a value. | |
// | |
// The define statement looks like this: #define NAME VALUE. Note that NAME can | |
// not contain spaces, because then it would be interpreted as VALUE. | |
// PINS: | |
// | |
// H-bridge | |
#define IN1 2 | |
#define IN2 3 | |
#define IN3 4 | |
#define IN4 5 | |
#define ENA 6 | |
#define ENB 7 | |
// CONSTANTS | |
// | |
// motor speeds | |
#define FAST 1023 | |
#define STOP 0 | |
// A function is a piece of code that can be "called" from anywhere and give | |
// back a result. A function "signature" tells the Arduino how to use it. | |
// A signature looks like this: | |
// type name(type1 parameter1, type2 parameter2) { | |
// code | |
// } | |
// The type is the type of value the function gives back. The name is what we | |
// can use to call it. "Parameters" are values given to the function. Think of | |
// it like math: f(x) = x + 2. Here, you give "f" a value, that it calls "x", | |
// a number, and you get a number (x + 2) back. Each parameter must have it's | |
// type specified, as types are very important to the computer. | |
// | |
// Here, we want to define a function that moves the motors. We don't expect | |
// any values back from it (just a physical reaction), we can call it "drive" | |
// and we would need to give it two values: direction (forwards? true or false) | |
// and the speed (a number -- can be decimal -- from 0 to 1023). | |
void drive(bool clockwise, float speed) { | |
// Now we have a true/false value ("boolean", or `bool` for short) and a | |
// number (that can be a decimal) -- a "floating point value", or `float`. | |
// Knowing how H-bridges work, we can program that in here. | |
// First, the speed. We use `analogWrite` to write a value to a pin that's | |
// anywhere between 0 and 1023. It goes: analogWrite(pinNumber, value); | |
analogWrite(ENA, speed); | |
// Next, we take care of the switches. We need to send a digital signal to | |
// the pins, using `digitalWrite`. It goes: digitalWrite(pin, HIGH/LOW) | |
// | |
// Now, here we are using something called a "tertiary operator". It can | |
// choose between multiple values based on a condition. It looks like this: | |
// condition ? ifTrue : ifFalse. | |
// Here, we want to send HIGH or low based on the direction. So: | |
digitalWrite(IN1, clockwise ? HIGH : LOW); | |
digitalWrite(IN2, clockwise ? LOW : HIGH); | |
} | |
// This is the setup function. Arduino knows to run this code by itself when | |
// it turns on for the first time. It will only run once, so put all setup | |
// code here, and don't expect it to keep being called. | |
void setup() { | |
// This allows us to read and print to the Serial Monitor. 9600 just tells | |
// the Arduino how to communicate with the Serial Monitor (like a TV channel) | |
Serial.begin(9600); | |
// We need to explicitly configure all our pins to being output pins | |
// We can call `pinMode` to do it. It looks like this: | |
// pinMode(pinNumber, typeOfMode); | |
// Here, we use the pin number from the `define`s, and OUTPUT for the mode. | |
pinMode(IN1, OUTPUT); | |
pinMode(IN2, OUTPUT); | |
pinMode(IN3, OUTPUT); | |
pinMode(IN4, OUTPUT); | |
pinMode(ENA, OUTPUT); | |
pinMode(ENB, OUTPUT); | |
} | |
// This is the `loop` function. Just like the setup function, Arduino knows to | |
// call this whenever it can. So put your "always running" logic here. | |
void loop() { | |
// Let's go simply back and forth a bit. | |
// | |
// First, we go forward for two seconds. | |
drive(true, FAST); | |
delay(2000); | |
// Next, stop. Notice the direction doesn't matter here | |
drive(true, STOP); | |
// Now, drive backwards for two seconds. | |
drive(false, FAST); | |
delay(2000); | |
// That's it! See how much easier it was, | |
// now that we made our own `drive` function? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment