I am using an Arduino pro, zx gesture sensor and the SI4703 evalution FM Radio board for this project. My intention is to change radio channels with each swipe (left or right). The variable x in my code determines the radio channel. However, when I swipe my hand right the first time, the variable x jumps from 0 to 3! (bypassing 1 and 2) I was wondering why this is so. It appears that there may be a loop that causes x++ to repeat until x=3? But then again, shouldn’t the ‘break’; in the code prevent the switch statement from repeating? It would be great if someone could help me by pointing out where my error is and how I can correct it.
My code is shown below:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <ZX_Sensor.h>
#include <Si4703_Breakout.h>
// Global Variables
SoftwareSerial soft_serial(10, 11); // RX, TX
uint8_t c;
int resetPin = 2;
int SDIO = A4;
int SCLK = A5;
Si4703_Breakout radio(resetPin, SDIO, SCLK);
int channel;
int volume;
char rdsBuffer[10];
int x = 0;
void setup() {
// Initialize hardware serial port. Note the baud rate.
Serial.begin(115200);
Serial.println();
Serial.println("-----------------------------------------");
Serial.println("SparkFun/GestureSense - UART Gesture Demo");
Serial.println("Note: higher 'speed' numbers mean slower");
Serial.println("-----------------------------------------");
// Initialize software serial port
soft_serial.begin(115200);
radio.powerOn();
radio.setVolume(0);
}
void loop() {
// Read in a character from the UART
if ( soft_serial.available() ) {
c = soft_serial.read();
// Determine type of message and print it
switch ( c ) {
case ZX_UART_GESTURE:
// Determine type of gesture
c = soft_serial.read();
switch ( c ) {
case RIGHT_SWIPE:
c = soft_serial.read();
Serial.print("Right Swipe");
//Serial.println(c, DEC);
Serial.print(x);
myswiperightfunction();
break;
case LEFT_SWIPE:
c = soft_serial.read();
Serial.print("Left Swipe");
//Serial.println(c, DEC);
Serial.print(x);
myswipeleftfunction();
break;
case UP_SWIPE:
c = soft_serial.read();
Serial.print("Up Swipe");
Serial.println(c, DEC);
volume ++;
if (volume == 16) volume = 15;
radio.setVolume(volume);
default:
break;
case ZX_UART_Z:
Serial.print("Z: ");
c = soft_serial.read();
Serial.println(c, DEC);
}
break;
}
}
}
void myswiperightfunction(){
radio.setVolume(1);
x++;
if (x = 1) {
channel = 915; // Station 1
radio.setChannel(channel);
}
if (x = 2) {
channel = 1003; // Station 2
radio.setChannel(channel);
}
if (x = 3) {
channel = 1019; // Station 3
radio.setChannel(channel);
}
}
void myswipeleftfunction(){
radio.setVolume(1);
x--;
if (x = 1) {
channel = 915; // Station 1
radio.setChannel(channel);
}
if (x = 2) {
channel = 1003; // Station 2
radio.setChannel(channel);
}
if (x = 3) {
channel = 1019; // Station 3
radio.setChannel(channel);
}
}