Hello,
I am constructing an electric longboard type of project with the following desired functionality:
- A load cell (0-100lbs) connected to my Arduino UNO is the only analog input
- Based on the load input the following should execute:
- LEDs should light up progressively; green gets brighter up to 30lbs and stays on past that, yellow starts at 30lbs and gets brighter to 50lbs and red goes from 50 to 100.
- My motor (electric skateboard BLDC with VESC) should kick in progressively at any force above ~30lbs
- Optional: Have a buzzer (piezo) that sounds over 50lbs
Background:
- I have run this motor with the VESC off the Arduino before so that is not an issue. My issue is mainly related to the programming and why the functionality stops or changes when new features are added.
Good news:
- I had achieved the LED functionality I was looking for with just the load cell and LEDs.
Bad news:
- The second I use the esc.attach(#) function for my motor it breaks my LED code, and the LEDs now only respond to the higher bracket, i.e. green will come on at 30lbs and yellow at 50 and so on.
- When I try to attach just the buzzer, it will sound but will not allow the red LED to light up
Here is my code thus far:
Note: I have a pretty basic structure and planned to move the tasks into functions to clean it up once it was all working.
#include <Servo.h> //Using servo library to control ESC
Servo esc; //Creating a servo class with name as esc
int LoadCellPin = 0; // Load Cell is connected to analog 0
int GLEDpin = 9; // connect Green LED to pin 9 (PWM pin)
int YLEDpin = 10; // connect Yellow LED to pin 10 (PWM pin)
int RLEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LoadCellReading; // the analog reading from the Load cell
int GLEDbrightness;
int YLEDbrightness;
int RLEDbrightness;
int Force;
int Speed;
#define buzzer 6
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(GLEDpin, OUTPUT);
pinMode(YLEDpin, OUTPUT);
pinMode(RLEDpin, OUTPUT);
pinMode(buzzer, OUTPUT);
//esc.attach(5); //Specify the esc signal pin,Here as D8 <<<<*** When I uncomment this line my working LED functions no longer work
//esc.writeMicroseconds(1000); //initialize the signal to 1000
}
void loop(void) {
LoadCellReading = analogRead(LoadCellPin);
Force = map(LoadCellReading, 100, 999, 0, 100);
// <<<<*** These lines are to send the signal to the motor and can be added back in once the code is stable
//Speed = map(LoadCellReading, 300, 999,1000,2000); //mapping val to minimum and maximum(Change if needed)
//if (Speed >= 1000) {
//esc.writeMicroseconds(Speed); //using val as the signal to esc
//}
Serial.print("Analog reading: ");
Serial.print("Load Cell = ");
Serial.print(LoadCellReading);
Serial.print("\t Force (lbs) = ");
Serial.println(Force);
Serial.print("Speed = ");
Serial.println(Speed);
if (Force <= 30) {
GLEDbrightness = map(Force, 0, 30, 0, 255);
analogWrite(GLEDpin, GLEDbrightness);
digitalWrite (YLEDpin, LOW);
digitalWrite (RLEDpin, LOW);
noTone(buzzer);
}
else if (30 < Force && Force <= 50) {
YLEDbrightness = map(Force, 30, 50, 0, 255);
analogWrite(YLEDpin, YLEDbrightness);
digitalWrite (GLEDpin, HIGH);
digitalWrite (RLEDpin, LOW);
noTone(buzzer);
}
else if (50 < Force && Force <= 100) {
RLEDbrightness = map(Force, 50, 100, 0, 255);
analogWrite(RLEDpin, RLEDbrightness);
digitalWrite (GLEDpin, HIGH);
digitalWrite (YLEDpin, HIGH);
//tone(buzzer, 2000); <<<< ***This line is commented out because it stops the Red LED from turning on, and if I put this before RLEDbrightness it makes a weird electronic noise
}
delay(100);
}
Quick wiring overview:
- Load cell has 3 wires going to GND, 5V, and A0
- Motor ESC has 3 wires going to GND, Vin, and ~5 (Digital in)
- 3 LEDs coming from ~9, ~10, ~11, each going to GNd with a 220ohm resistor
- Piezo coming form ~6 and going to GND, no resistors
I appreciate any advice I can get on running the motor, LEDs, and potentially the buzzer at the same time.
If you require any further information to help with the trouble shooting I am happy to provide it.
Thank you