Hello Arduino Community,
Currently I’m working on a Geocache cube. there is a (capacitive) touchpad made from € coins. When a coin lights up you need to touch it. Now another coins lights up and you touch it,and another etc etc.. if you are fast enough and finish all the rounds the cube opens (servo lever touches open mechanism). when you are to slow the servo touches a mechanical endswitch and cuts the power and you have to start over from round 1.
I’m using two arduino nano’s that are communicate via the serial port (TX>RX and RX>TX).
the first nano is connected to the touchpad and sends characters A,B,C,D,E,F,G,H,I to the second nano. A when led/coin 1 is touched, B when led/coin 2 is touched etc. (see first sketch)
the second nano processes the input from nano 1 and also controls the LED’s and Servo. (see second sketch)
everything went well but now I got stuck on the following:
In round 1 the characters A,B,C are sent to the second nano. When round 2 starts the last character send is still in the serial buffer from the second nano (see screenshot serial monitor nano 2).
so the statement below kicks in, the servo cuts the power and the game ends without a mistake being made.
if (incomingByte == 'A' || incomingByte == 'B' || incomingByte == 'C' || incomingByte == 'D' || incomingByte == 'E' || incomingByte == 'F')
{
Serial.print(incomingByte);
servo1.write(20);
}
is there a way to clear the serial buffer after round one? ore does someone know another smart solutions to make this game work?
MB
Sketch 1#
#include <CapacitiveSensor.h>
CapacitiveSensor cs_2_3 = CapacitiveSensor(2,3);
CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor cs_2_5 = CapacitiveSensor(2,5);
CapacitiveSensor cs_2_6 = CapacitiveSensor(2,6);
CapacitiveSensor cs_2_7 = CapacitiveSensor(2,7);
CapacitiveSensor cs_2_8 = CapacitiveSensor(2,8);
CapacitiveSensor cs_2_9= CapacitiveSensor(2,9);
CapacitiveSensor cs_2_10= CapacitiveSensor(2,10);
CapacitiveSensor cs_2_11= CapacitiveSensor(2,11);
void setup()
{
cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);
Serial.begin(9600);
}
void loop()
{
long start = millis();
long total1 = cs_2_3.capacitiveSensor(30);
long total2 = cs_2_4.capacitiveSensor(30);
long total3 = cs_2_5.capacitiveSensor(30);
long total4 = cs_2_6.capacitiveSensor(30);
long total5 = cs_2_7.capacitiveSensor(30);
long total6 = cs_2_8.capacitiveSensor(30);
long total7 = cs_2_9.capacitiveSensor(30);
long total8 = cs_2_10.capacitiveSensor(30);
long total9 = cs_2_11.capacitiveSensor(30);
if(total1 > 200){
Serial.print('A');
delay(1);
}
if(total2 > 200){
Serial.print('B');
delay(1);
}
if(total3 > 200){
Serial.print('C');
delay(1);
}
if(total4 > 200){
Serial.print('D');
delay(1);
}
if(total5 > 200){
Serial.print('E');
delay(1);
}
if(total6 > 200){
Serial.print('F');
delay(1);
}
if(total7 > 200){
Serial.print('G');
delay(1);
}
if(total8 > 200){
Serial.print('H');
delay(1);
}
if(total9 > 200){
Serial.print('I');
delay(1);
}
}
Sketch 2#
char incomingByte;
void setup() {
// first round of the game, LED's 1,2 and 3 start burning. Touch buttons 1,2 and 3 need to be pushed to proceed
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
while(true){
if (Serial.available() > 0) {
incomingByte = Serial.read(); // check for incoming data
if (incomingByte == 'A') {
digitalWrite(LED1, LOW);
}
if (incomingByte == 'B') {
digitalWrite(LED2, LOW);
}
if (incomingByte == 'C') {
digitalWrite(LED3, LOW);
}
if(!digitalRead(LED1)&!digitalRead(LED2)&!digitalRead(LED3))
{
break;
}
}
}
// second round of the game. LED's 7,8,9 start burning. Touch buttons 7,8 and 9 need to be pushed to proceed.
//when a mistake is made (touch button 1,2,3,4,5 or 6 are touched) the servo rotates and cuts the power to both arduino's to stop the game.
Serial.print(incomingByte);
Serial.println();
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);
while(true){
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'G') {
digitalWrite(LED7, LOW);
}
if (incomingByte == 'H') {
digitalWrite(LED8, LOW);
}
if (incomingByte == 'I') {
digitalWrite(LED9, LOW);
}
if(!digitalRead(LED7)&!digitalRead(LED8)&!digitalRead(LED9))
{
break;
}
if (incomingByte == 'A' || incomingByte == 'B' || incomingByte == 'C' || incomingByte == 'D' || incomingByte == 'E' || incomingByte == 'F')
{
Serial.print(incomingByte);
servo1.write(20);
}
void loop() {
}