Search code examples
arduinoarduino-unoapp-inventor

I'm trying to power up a toy motor using 3 different velocities like a fan would


I'm trying to power up one of those tiny toy motors like this one. And I'm trying to make it use 3 different velocities. I made an app in app inventor and trying to control the speed of the motor with a fan attached to it. I'm using the bluetooth HC-05 and I've already configured the bluetooth and used it to turn on/off 3 LEDs and 1 of those tiny motors and it worked just fine. The bluetooth connects correctly and everything. But now after a week of not using my arduino I tried to control 3 different speeds and the motor just turns on at max speed no matter if i press the lower speed or the higher speed button, it doesn't even turn off when i press off.

I don't understand what's happening. Last time it worked ok. Bluetooth device has the same name when I connect to it. I'm not using resistors since it woked without them last time and it's just a toy motor.

This is my arduino code:

#include <SoftwareSerial.h>
SoftwareSerial myBT(10,11);
char DATA = 0;
int FAN = 4;
int FAN2 = 7;
int LED = 8;
void setup() {
  myBT.begin(38400);
  pinMode(FAN,OUTPUT);
  pinMode(FAN2,OUTPUT);
  pinMode(LED,OUTPUT);
}

void loop() {
 if(myBT.available()){
   DATA = myBT.read();

   if(DATA == '1')
   digitalWrite(FAN,30);
   if(DATA == '2')
   digitalWrite(FAN,120);
   if(DATA == '3')
   digitalWrite(FAN,254);
   if(DATA == '9')
   digitalWrite(FAN,LOW);

   if(DATA == '4')
   digitalWrite(FAN2,80);
   if(DATA == '5')
   digitalWrite(FAN2,160);
   if(DATA == '6')
   digitalWrite(FAN2,254);
   if(DATA == '9')
   digitalWrite(FAN2,LOW);

   if(DATA == '7')
   digitalWrite(LED,HIGH);
   if(DATA == '8')
   digitalWrite(LED,LOW);
  }
}

And this is my designer enter image description here and my blocks enter image description here.

Do you think there's anything wrong with the code? Or is it maybe that I connected something wrong?


Solution

  • digitalWrite(FAN,30);
    

    digitalWrite expects HIGH or LOW. Anything non zero just sets the pin HIGH. Did you mean to use analogWrite here?

    You don't mention what type of Arduino you have, but if it is the common UNO then pins 4 and 7 don't support PWM so analogWrite won't work on them. You'd have to use pins that are PWM capable. They have the ~ next to them on the board.