I have recently bought a 12V DPDT relay and 12V Brush-less DC motor. I have tested both the relay and motor separately and both of them work. My issue is once I try to combine both codes to control the relay and motor individually, the components (relay and motor) no longer respond as needed. The relay would activate, but the motor does not start. Another issue is the relay does not respond when I try to de-energize it.
The code below is an initial attempt in writing the code for this system. Is there any issue with this code? Or something I should add?
The goal of this system is to reverse the direction of the motor using a relay.
Thanks
// Code
#include <Servo.h>
char val; // variable to receive data from the serial port
int ledpin = 11; // LED connected to pin 11 (on-board LED)
int value1 = 1500; // set values you need to zero
int value2 = 1500; // set values you need to zero
Servo firstESC;
void setup()
{
pinMode(ledpin = 11, OUTPUT); // pin 11 (on-board LED) as OUTPUT
firstESC.attach(9);
Serial.begin(9600); // start serial communication at 1151100bps
}
void loop()
{
firstESC.writeMicroseconds(value1);
if( Serial.available() ) // if data is available to read
val = Serial.read(); // read it and store it in 'val'
if( val == 'a' ) // if 'a' was received led 11 is switched off
{
digitalWrite(ledpin = 11, HIGH); // turn Off pin 11
value1 = Serial.parseInt();
}
if( val == 'd' ) // if 'd' was received led 11 on
{
digitalWrite(ledpin = 11, LOW); // turn ON pin 11
value2 = Serial.parseInt();
}
}