Hello, thank you for your time. I am fairly new to programming but I think I have a decent understanding. I am not a pro so please forgive my ignorance.
With that being said here is a rundown of what I am trying to do and what issues I am hoping to have resolved with the community help.
NOTE: I have working code for each sensor and have tested it individually but I am unable to incorporate all the sensors into one large project.
What I am trying to do...
Open Bluetooth window via Teraterm. Type various instructions:
1: Choose the color of an LED by typing the following in the Teraterm window (Bluetooth) "LED 255,0,0." to turn on the RGB led and change it to red. (Or any color)
2: Ping GPS and receive a NMEA sentence. Send that information back to the computer via Bluetooth.
3: Get IMU information continuously until a stop command is received. Send that information back to the computer via Bluetooth.
Please correct me if I am wrong but the way I would think you would go about doing this is to have a main loop that would select a loop for each instruction. For example, when I want to read the GPS I would have something like "If (BluetoothData == 'GPS')GPS(); Then in the GPS loop I would have the instructions to read the GPS and send it out via bluetooth.
Now this is quite a lot and I am struggling with receiving the bluetooth string and selecting the loop. I have since broken this down to simply control an LED until I can figure out exactly what I am missing.
I am suspecting that my issues lie within "char BluetoothData" and using that alias to parse inside of the LED loop?
I have been searching various forums but haven't found exactly how to resolve this issue.
Any help would be greatly appreciated! This is for my Senior Project and it's midterms... so forgive me if I am asking a 'stupid' question! Thanks again!
char BluetoothData = 0;
const int redPin = 9; // PWM Pin for Red LED
const int bluePin = 10; // PWM Pin for Blue LED
const int greenPin = 11; // PWM Pin for Green LED
void setup() {
Serial.begin(115200); //serial port to the computer
Serial3.begin(115200); //serial port 3 (Tx 15, Rx 14) for the bluetooth
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.println("Online");
Serial3.println("Online");
}
void loop() {
if (Serial3.available()) {
BluetoothData = Serial3.read();
if (BluetoothData == 'l')LED();
}
}
void LED() {
Serial.println("LED Loop");
// if there's any serial available, read it:
if (Serial3.available()) {
// look for the next valid integer in the incoming serial stream:
int Red_value = Serial3.parseInt();
// do it again:
int Green_value = Serial3.parseInt();
// do it again:
int Blue_value = Serial3.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial3.read() == '\n') {
// constrain the values to 0 - 255
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
Red_value = constrain(Red_value, 0, 255);
Green_value = constrain(Green_value, 0, 255);
Blue_value = constrain(Blue_value, 0, 255);
// change the Red, green, and blue legs of the LED:
analogWrite(redPin, Red_value);
analogWrite(greenPin, Green_value);
analogWrite(bluePin, Blue_value);
// print the three numbers in one string as hexadecimal:
Serial.print(Red_value);
Serial.print(Green_value);
Serial.println(Blue_value);
Serial3.print(Red_value);
Serial3.print(Green_value);
Serial3.println(Blue_value);
}
}
}