Can Someone Look Over This Code, Please?

I’m trying to control a DC motor thru a mobile app with a range slider, but I cannot get it to work. I’m wondering if someone could look over the code below and tell me if it should work or not. The main.ino verifies correctly, so I’m guessing it’s in the mobile app?..
BTW, I didn’t write this code, it’s an open source project. I can’t write code, therefore I can’t find the problem.

#include "FastLED.h"

#define MOTOR_PIN D1

int motorPower = 0; 

void setup() 
    pinMode(MOTOR_PIN, OUTPUT);
    Particle.function("setPower", setPower);

void loop() 
  { }
    int setPower(String power) {
    motorPower = power.toInt();
    analogWrite(MOTOR_PIN, motorPower);

I could only find 2 references to the motor in the mobile app. One is in ‘device.html’ and the other is in file ‘Controller.js’. Perhaps the problem is with the range slider itself?

<ion-view view-title="{{ }}">
  <ion-content class="padding">
    <div class="item item-divider">Bubble Volume</div>
    <div class="item range range-energized">
      <i class="icon ion-android-remove"></i>
      <input type="range" min="0" max="255" value="0" on-release="setBubbles()" ng-model="device.MOTOR_PIN">
      <i class="icon ion-android-add"></i>
angular.module('starter.controllers', [])

.controller('DeviceController', function($scope, $stateParams, Devices) {
    $scope.device = {
    power: 0

    $scope.setBubbles = function() {$, "setPower", $scope.device.power);

I’m thinking it would most likely be in the Controller.js. I’ve got my breadboard set up exactly as the schematic in the project shows. I’d love some expert opinions so I could get on with my project.

This is bound to be wrong.

void loop() 
    int setPower(String power) {
    motorPower = power.toInt();
    analogWrite(MOTOR_PIN, motorPower);

There is one closing curly brace missing.

void loop() { }
int setPower(String power) {
    motorPower = power.toInt();
    analogWrite(MOTOR_PIN, motorPower);

    return motorPower;

To check whether the firmware works or not I’d use
to send some valueto setPower() and see if the motor works.

Thanks for the quick response. I’m coming up with nothing.

What does that mean? Did you try using the console (or the CLI if you have that installed) to send a value to your function? If so, what was the result.

BTW, you should be returning an int from your setPower function.


Good point, I've corrected that in my snippet :thumbsup:

Sorry, that means I still don’t have power to the motor. Here’s something interesting in the Logs. Where is the ‘power-down’ coming from?

Thanks ScruffR. I’ve added the return, but it’s still not working.

Your power-down reset might indicate that you’ve got a power issue when the motor kicks in.

What motor are you using and how are you powering it?
How does your wiring look?

This is the schematic for the project I’m working on. All the parts I’m using are the exact same ones suggested by the tutorial: I’m using a 5V 2A power supply.

You could do with some filtering on your Photon supply as I’d suspect the motor to introduce a considerable amount of noise on the power rails.
With inductive loads - like a motor - you should also add a flyback diode to prevent damaging the Photon via the kickback on the supply rails.

With this in general, what does the Photon do when you start up first and then when you send the first control command (e.g. 128 = 50% duty cycle)?

Thanks for the suggestion. I do remember reading somewhere about using a flyback diode. I don’t have one on hand right now, but I’m gonna order some.
If I’m using your example the right way (you’re dealing with a real newbie at this IoT stuff), this is what it says:

but the motor isn’t responding to nothing.

Maybe your transistor doesn’t switch properly.
To see whether your pin switches correctly or not you could add an LED (with a current limiting resistor) from D0 to GND.
Or even better if you’ve got an oscilloscope.

Well, I think I’m finally starting to make some progress. I now believe the problem is in the hardware. I was using a photon soldered to a Neopixel ring for the light shows in the sketch. Then I ordered another photon & Neopixel ring, this time not soldering them together, and I can get the pump to work. It’s not working exactly as it should; I can only start and stop it, I can’t adjust the speed. Any thoughts on that?

Send to Dropbox, Google drive or Github…

[quote=“slarja, post:13, topic:32984, full:true”]I can only start and stop it, I can’t adjust the speed. Any thoughts on that?

Are you certain your transistor is switching fast enough? I’ve ditched all trannies for Powermosfets in most of my applications. Also, you really do want that fly back diode, you may have damaged the tranny already.