Hi, the problems I'm getting with this project are getting weirder and weirder.
The program takes an input from serial (A component name) and searches through a 3D array of strings to find a match. The coordinates from the array represents the position of the draw. I.e. coords (0, 0) represent the top left drawer and a laser is pointed at it. The last dimension is used for when there are multiple things in the draw but both will output the same position (variable name : pos)
My problem is that the arduino restarts (I'm assuming this because it prints a line from "void setup()") when I input a phrase (i.e. screws). The output to serial moniter is:
|sāø®
Hello
I changed my code to use PROGMEM because before when stored in SRAM the component_array was filled to its appropriate size (i.e. all the components were in the array) it would stop working (The global variables still only used 60% of SRAM). It stopped working because Serial.readString() stopped working but it would not reset.
However when only one row was in the array (while keeping the initialised size to component_array[5][6][2]) The code would find the position fine.
I printed "|" on either side of input so I could see if there were any spaces or \n.
String input = "unchanged";
byte pos[2] = {0, 0};
bool part_found = false;
void setup() {
Serial.begin(9600);
Serial.println("\nHello\n");
yServo.attach(9);
xServo.attach(10);
pinMode(6, OUTPUT);
} // setup()
void loop() {
while (Serial.available() == 0) {
}
input = Serial.readString();
input = formatInput(input);
Serial.print("|");
Serial.print(input);
Serial.println("|");
findPos(input);
if (part_found) {
Serial.print("Row: ");
Serial.print(pos[0]);
Serial.print("\tColumn: ");
Serial.println(pos[1]);
pointLaser();
} else {
Serial.println("Part not found.");
}
input = "";
} // loop()
void findPos(String input) {
part_found = false;
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
strcpy_P(buffer1, (char *)pgm_read_word(&(component_array[i][j][0]))); // Necessary casts and dereferencing, just copy.
// Serial.println(buffer1);
strcpy_P(buffer2, (char *)pgm_read_word(&(component_array[i][j][1]))); // Necessary casts and dereferencing, just copy.
// Serial.println(buffer2); // Serial.print("Input: |");
// Serial.print(input);
// Serial.print("|\tComponent 1: |");
// Serial.print(component_array[i][j][0]);
// Serial.print("|\tComponent 2: ");
// Serial.println(component_array[i][j][1]);
if (input == buffer1 || input == buffer2) {
pos[0] = i + 1;
pos[1] = j + 1;
part_found = true;
// Break
i = NUM_ROWS;
j = NUM_COLS;
}
}
}
} // findPos()
void pointLaser(){
byte angle_y = map(pos[0], 1, NUM_ROWS, Y_TL, Y_BL);
byte angle_x = map(pos[1], 1, NUM_COLS, X_TL, X_TR);
yServo.write(angle_y);
xServo.write(angle_x);
delay(100);
analogWrite(6, 8);
delay(5000);
analogWrite(6, 0);
}
String formatInput(String input) {
input.toLowerCase();
byte input_last_char_index = input.length() - 1;
if (input.charAt(input_last_char_index) == '\n' || input.charAt(input_last_char_index) == '\r' ) // Remove new line character at end
input.remove(input_last_char_index);
--input_last_char_index;
if (input.charAt(input_last_char_index) == '\n' || input.charAt(input_last_char_index) == '\r' ) // Remove new line character at end
input.remove(input_last_char_index);
return input;
}
// const String component_array[NUM_ROWS][NUM_COLS][2] = {
// {{"resistors", "resistors"}, {"lm6uu", "hinges"}, {"capacitors", "inductors"}, {"ldrs", empty_spot}, {"power resistors","power resisters"}, {"leds", empty_spot}},
// {{"desoldered parts", "fuses"}, {"barrel connectors", "vgas"}, {"capacitors", "salvaged parts"}, {"rotary encoders", "crocodile clips"},
// {"screw terminals", "female headers"}, {"male headers", "xt60 connectors"}},
// {{"female barrel connectors", "earphone cables"}, {"photointerrupters", "optical encoders"}, {"gold", "cable organizers"},
// {"rubber feet", "fishing set"}, {"shelf holders", "motion resistors"}, {"power transistors", empty_spot}},
// {{"wifi stuff", empty_spot}, {"headphone jack", empty_spot}, {"bridge rectifier", "battery protectors"}, {"joysticks", "watch parts"},
// {"diodes", "diodes"}, {"mosfets", "mosfets"}},
// {{"optics", "dvd parts"}, {"magnets", "magnets"}, {"springs", "springs"}, {"aaa batteries", "small bolts"}, {"screws", "large bolts"},
// {"transistors", "tiny bolts"}}
// };