//add Si4703 Library to sketch

#include <Si4703_Breakout.h>
#include <Wire.h>

int resetPin = 2;   // reset on digital pin 2
int SDIO = A4;      //SDIO on analogue pin 4
int SCLK = A5;      //SCLK on analogue pin 5

// define radio variables
Si4703_Breakout radio(resetPin, SDIO, SCLK);
int channel = 878;
int volume = 2;
int newChannel = 0;
int newVolume = 0;
char printBuffer[50];

//register channel switches here
const int northSwitch = 3;    //North switch is on digital pin 3
const int eastSwitch = 4;
const int southSwitch = 5;
const int westSwitch = 6;

//register volume switches here
const int volume1 = 7;          //volume1 is on digital pin 7
const int volume2 = 8;
const int volume3 = 9;
const int volume4 = 10;

//current reading from the input pins
int northState;    
int eastState; 
int southState;
int westState;

int volume1_State;
int volume2_State;
int volume3_State;
int volume4_State;

void setup() {
 
  // initialize channel switches as inputs
  pinMode(northSwitch, INPUT);          
  pinMode(eastSwitch, INPUT);
  pinMode(southSwitch, INPUT);
  pinMode(westSwitch, INPUT);
  
  pinMode(volume1, INPUT);
  pinMode(volume2, INPUT);
  pinMode(volume3, INPUT);
  pinMode(volume4, INPUT);
  
  radio.powerOn();
  radio.setVolume(volume);
  radio.setChannel(channel);
 
  Serial.begin(9600);
}

void loop() 
{
    northState = digitalRead(northSwitch);
    eastState = digitalRead(eastSwitch);
    southState = digitalRead(southSwitch);
    westState = digitalRead(westSwitch);
    
    volume1_State = digitalRead(volume1);
    volume2_State = digitalRead(volume2);
    volume3_State = digitalRead(volume3);
    volume4_State = digitalRead(volume4);
    
    
    Serial.print("northState:");
    Serial.println(northState);

    Serial.print("eastState:");
    Serial.println(eastState);
    
    Serial.print("southState:");
    Serial.println(southState); 
    
    Serial.print("westState:");
    Serial.println(westState);
    
    Serial.print("volume1_State:");
    Serial.println(volume1_State);
    
    Serial.print("volume2_State:");
    Serial.println(volume2_State);
    
    Serial.print("volume3_State:");
    Serial.println(volume3_State);
    
    Serial.print("volume4_State:");
    Serial.println(volume4_State);
  
    
    if (northState == HIGH) {
      newChannel = 883; //WRCT
    } 
      else if (eastState == HIGH) {
        newChannel = 1001;
      }
      else if (southState == HIGH) {
        newChannel = 1047;
      }
      else if (westState == HIGH) {
        newChannel = 961;
      }
      
    else {
      newChannel = 878; //static channel
    }
    
    if (newChannel != channel) {
      channel = newChannel;
      radio.setChannel(channel);
    }

  if (volume1_State == HIGH) {
      newVolume = 6;
    } 
    else if (volume2_State == HIGH) {
      newVolume = 10;
    }
    else if (volume3_State == HIGH) {
      newVolume = 12;
    }
    else if (volume4_State == HIGH) {
      newVolume = 16;
    }

  else {
      newVolume = 1; //static channel
    }
    
    if (newVolume != volume) {
      volume = newVolume;
      radio.setVolume(volume);
    }

}