And it has been changed a bit so that just integers can be parsed. This all works great and parses the data correctly and as intended. However when I send the data from the bluetooth app on my phone to control some pumps, it doesnt work for some reason. I know 100% it is not to do with the wiring because I ran a different sketch before and the pump turns on.
Below is my full code and a screen shot showing that it enters the if statement and parses the data as intended.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
int Shots = 0;
int Glass_Size = 0;
int pump_num = 0;
boolean newData = false;
//============
//LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD
void setup() {
Serial.begin(9600);
//lcd.init();
//lcd.backlight();
}
//============
void loop() {
//lcd.setCursor(1, 0); // Set the cursor on the third column and first row.
//lcd.print("Please Pick A");
//lcd.setCursor(1, 1); //Set the cursor on the third column and the second row (counting starts at 0!).
//lcd.print("Drink from App");
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
if (pump_num == 2)
{
//lcd.setCursor(1, 0);
//lcd.print("Your Drink is");
//lcd.setCursor(1, 1);
//lcd.print("Being Made");
digitalWrite(2, HIGH);
delay(2000);
digitalWrite(2, LOW);
Serial.print("test");
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
pump_num = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
Shots = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Glass_Size = atoi(strtokIndx);
}
//============
void showParsedData() {
Serial.print("Pump Number ");
Serial.println(pump_num);
Serial.print("Shots ");
Serial.println(Shots);
Serial.print("Glass Size ");
Serial.println(Glass_Size);
}
Yes, the screen shot does show "test" which means pin 2 is going HIGH/LOW, but what is it connected to? How do you have this all wired up? A schematic (hand drawn is okay) would be helpful. Often, it is a missing ground connection between everything or a power issue.
This is the only bit of code that produces "test", so if you see it on the monitor, the other things are happening as well = pin2 high, wait, pin2 low, print
If you don't want to provide a schematic or let anyone know what pin 2 is connected to, then I can't help.
Excellent. And where does the +5V come from? Is the Arduino plugged into your PC and you are powering everything through the USB cable? Or do you have it connected to a wall wart to supply 6-9V to Vin?
Starting your pump could take a lot of initial current to get it going and exceed what can be delivered.
a 9V wall plug going into the barrel jack means the onboard regulator is supplying all the current and it can only do 500mA, max. It is better to get a 5V wall plug (phone charger) and connect that directly to the 5V on the board. This bypasses the voltage regulator and the max current is dictated by your wall plug (2A?)
The difference between those two sketches is that, in your first one, pump_num gets the value 2 and then retains that value every time through loop() so you always get the ON/delay/OFF to happen.
Your "test" sketch only turns on if something is available on Serial and that something is '2'