Hi,
I am currently working on a project in which I wish to control a DC motor with the tempo from an audio recording software (ableton). Through Max for Live I send Serial data (the tempo) as an integer value from 0 to 999 and above.
The idea is to have this unit in the studio and control it wirelessly from one room to another. The code works fine when not using wireless communication. But I decided to use the HC12 modules to get data from one side and send it all to the other room. Here is my code on the receiver side which controls the motor:
#include <SoftwareSerial.h>
SoftwareSerial HC12 (10, 11);
int motor1pin1 = 2;
int motor1pin2 = 3;
int LEDred = 5;
int LEDgreen = 6;
int LEDblue = 7;
void setup() {
Serial.begin(9600);
HC12.begin(9600);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(9, OUTPUT);
pinMode(LEDred, OUTPUT);
pinMode(LEDgreen, OUTPUT);
pinMode(LEDblue, OUTPUT);
}
void loop() {
while (HC12.available()){
int value1 = HC12.read();
Serial.write(value1);
if (value1 == 0) {
analogWrite(9, 0);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(LEDred, LOW);
digitalWrite (LEDgreen, LOW);
digitalWrite(LEDblue, HIGH);
}
else if ((value1 > 0) && (value1 < 55)) {
analogWrite(9, 55);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite (LEDgreen, LOW);
digitalWrite(LEDred, HIGH);
digitalWrite(LEDblue, LOW);
}
else if ((value1 >= 55) && (value1 <= 254)) {
analogWrite(9, value1);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(LEDred, LOW);
digitalWrite(LEDgreen, HIGH);
digitalWrite(LEDblue, LOW);
}
else if (value1 >= 255) {
analogWrite(9, 255);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(LEDred, HIGH);
digitalWrite (LEDgreen, LOW);
digitalWrite(LEDblue, LOW);
}
}
}
Basically the motor should receive values only between "55" and "255" since below it does not turn and above it is no longer relevant. "0" stands for "stop", "0" to "54" should be "55", "55" to "255" should use the tempo value from the software and above "255" should remain "255" being the maximum speed.
The LED gives a visual understanding of the current situation to be able to quickly solve it from the audio software ( increasing the tempo value or decreasing it).
On the other side I just use a code I found on the internet ,which seems to work seemlessly, to send values through the Serial Monitor:
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
}
void loop() {
while (HC12.available()) { // If HC-12 has data
Serial.write(HC12.read()); // Send the data to Serial monitor
}
while (Serial.available()) { // If Serial monitor has data
HC12.write(Serial.read()); // Send that data to HC-12
}
}
The issue I am facing at the moment is that regardless of the value being sent wirelessly only the part
else if ((value1 > 0) && (value1 < 55)) {
analogWrite(9, 55);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite (LEDgreen, LOW);
digitalWrite(LEDred, HIGH);
digitalWrite(LEDblue, LOW);
becomes true and therefore the LED stays red and the motor runs slowly. BUT the Serial monitor displays the correct value (with Serial.write(value1)) when being inserted directly as follow:
void loop() {
// put your main code here, to run repeatedly:
while (HC12.available()){
int value1 = HC12.read();
Serial.write(value1);
if (value1 == 0) {
However when inserted in "else if" loops: "70" "80" and "90" only appear as "0" (also "170", "180" and "190").
Is there anything I am missing the code? is there any additional information regarding the HC12 or similar issues known?
I would be very grateful if anyone could have a look into it and eventually help me figuring it out ![]()
All the best!
ps: if you wonder what this is all for, this whole system aims at emulating the Leslie Cabinet effect on a microphone (recording phase instead of reproduction phase of a project).