08 – class notes, 21 Sep 2017 — Intro to Power and Motors

Timing

Two short sketches showing how we can use the timing commands to blink and LED on and off without using delay() statements.

This is the version suggested in class:

// -*-c++-*-
/*
The MIT License (MIT)

Copyright (c) 2017 J. Eric Townsend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


/*
* This is a simple example that turns the LED on and off based on a single time.
*/


int debugLed = 13;
int blinkTime = 500;

bool ledState = LOW;
int now = 0;
int previous = 0;

void setup() {
// put your setup code here, to run once:
pinMode(debugLed, OUTPUT);
Serial.begin(115200);
}

void loop() {
now = millis();

if ((now - previous) > blinkTime) {
ledState = !ledState;
digitalWrite(debugLed, ledState);
previous = now;
}

}

You could also use if/else

// -*-c++-*-
/*
The MIT License (MIT)

Copyright (c) 2017 J. Eric Townsend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


/*
* This is a simple state machine that tracks the state of the LED and
* allows it to be on and off for different lengths of time.
*/


int debugLed = 13;
int blinkTimeOn = 1000;
int blinkTimeOff = 1000;

int ledOn = 1;
int ledOff = 2;

bool ledState = LOW;
int now = 0;
int lastOn = 0;
int lastOff = 0;

void setup() {
// put your setup code here, to run once:
pinMode(debugLed, OUTPUT);
Serial.begin(115200);
}

void loop() {
now = millis();

if (!ledState && ((now - lastOff) > blinkTimeOff)) {
ledState = HIGH;
lastOn = now;
} else if (ledState && ((now - lastOn) > blinkTimeOn)) {
ledState = LOW;
lastOff = now;
}

digitalWrite(debugLed, ledState);
}

Driving Actuators

Motors, solenoids, and servos require different methods of control and often require external sources of power.

Here’s a solenoid-example with a Fritzing doc showing the schematic.  There’s a similar method for using a motor that we will go over on Tuesday.

The basics of using a servo that we looked at in class.

Leave a Reply