Hi all,
Maybe this is a dumb question, but I have this code for a WiFi spectrum analyzer I made, and it works well, but I have two problems. First, while the scan is running, it is a loop drawing the array of values onto the lcd. However, if I press the button (the interrupt routine) during the update, it doesnt change until the loop is finished. Is there a way to change the loop counter (variable n) from the interrupt, so it will advance to the new mode right away? Also, it takes a very long time to draw the array of values on the LCD, is there any way to reduce this? More information and a video are here: Arduino Multi-Tool (Spectrum analyzer, multimeter, slow oscilloscope) - Exhibition / Gallery - Arduino Forum
#include <rssi.h>
#include <SPI.h>
///Spectrum analyzer code for Arduino, CYWM6935, and PCD8544 Display.
///LCD Functions adapted from http://www.arduino.cc/playground/Code/PCD8544 thanks to the great community there!
///CYWM6935 Functions adapted from http://ea4eoz.ure.es/hsa.html thank you as well.
/////////////////////////////////////////////////////////////////
// Connections:
//Arduino pin 8 => Logic Level Translator => CYWM6935 Reset Pin
//Arduino pin 9 => Logic Level Translator => CYWM6935 SS Pin
//Arduino pin 11 => Logic Level Translator => CYWM6935 MOSI Pin
//Arduino pin 12 => CYWM6935 MISO Pin
//Arduino pin 13 => Logic Level Translator => CYWM6935 SCK Pin
//Radio PD pin -> 5v
//Arduino pin 6 => Logic Level Translator => LCD Reset pin
//Arduino pin 5 => Logic Level Translator => LCD DC pin
//Arduino pin 4 => Logic Level Translator => LCD SDIN pin
//Arduino pin 3 => Logic Level Translator => LCD SCLK pin
/////////////////////////////////////////////////////////////////
// LCD Pin Definitions and Variables
#define PIN_SCE 7
#define PIN_RESET 6
#define PIN_DC 5
#define PIN_SDIN 4
#define PIN_SCLK 3
#define LCD_VISIBLE_X_RES 84
#define LCD_C LOW
#define LCD_D HIGH
#define LCD_X 84
#define LCD_Y 48
#define LCD_CMD 0
#define OUT_OF_BORDER 1
#define PIXEL_ON 1
#define PIXEL_OFF 0
#define MAXCHANNEL 84
int bottom = 0;
#define MODECOUNT 5
int buttonPin = 2; // Mode-Changing Button
volatile int mode; //1 is normal, 2 is digital (stepping) mode, 3 is exposure mode
char signal[MAXCHANNEL];
unsigned char n=0;
int Lcd_TOP = 40;
int Lcd_visible = Lcd_TOP - bottom;
volatile bool toggled = 0;
rssi radio;
void setup() {
pinMode(buttonPin, INPUT); // button as input
digitalWrite(buttonPin, HIGH); // turns on pull-up resistor after input
// Hardware initialization
LcdInitialise();
LcdClear();
radio.RADIO_Init();
attachInterrupt(0, toggle, FALLING);
mode = 1;
}
void loop() {
if(toggled)
{
switchmode();
}
if (mode == 1)
{
gotoXY(75,5);
LcdCharacter('F');
gotoXY(69,5);
LcdCharacter('a');
gotoXY(63,5);
LcdCharacter('s');
gotoXY(57,5);
LcdCharacter('t');
gotoXY(51,5);
LcdCharacter(' ');
gotoXY(45,5);
LcdCharacter('S');
gotoXY(39,5);
LcdCharacter('c');
gotoXY(33,5);
LcdCharacter('a');
gotoXY(27,5);
LcdCharacter('n');
// Read signals
for (unsigned char n=0; n<MAXCHANNEL; n++){
// Read the signal on current channel
unsigned char s = radio.RADIO_RSSI(n);
s = map(s, 0, 31, 0,Lcd_visible);
signal[n]=s;
}
for (unsigned char n = LCD_VISIBLE_X_RES; n > 0; n--){
drawLine(n,Lcd_TOP,PIXEL_OFF);
drawLine(n,signal[abs(LCD_VISIBLE_X_RES-n)], PIXEL_ON);
}
}
else if (mode == 2)
{
gotoXY(75,5);
LcdCharacter('S');
gotoXY(69,5);
LcdCharacter('l');
gotoXY(63,5);
LcdCharacter('o');
gotoXY(57,5);
LcdCharacter('w');
gotoXY(51,5);
LcdCharacter(' ');
gotoXY(45,5);
LcdCharacter('S');
gotoXY(39,5);
LcdCharacter('c');
gotoXY(33,5);
LcdCharacter('a');
gotoXY(27,5);
LcdCharacter('n');
// Read signals
for (unsigned char n=0; n<MAXCHANNEL; n++){
// Read the signal on current channel
unsigned char s = radio.RADIO_RSSI(n);
s = map(s, 0, 31, 0,Lcd_visible);
if(s < signal[n])
{
signal[n] = signal[n]-1;
}
else {
signal[n]=s;
}
}
for (unsigned char n = LCD_VISIBLE_X_RES; n > 0; n--){
drawLine(n,Lcd_TOP,PIXEL_OFF);
drawLine(n,signal[abs(LCD_VISIBLE_X_RES-n)], PIXEL_ON);
}
}
else if (mode == 3)
{
gotoXY(75,5);
LcdCharacter('G');
gotoXY(69,5);
LcdCharacter('h');
gotoXY(63,5);
LcdCharacter('o');
gotoXY(57,5);
LcdCharacter('s');
gotoXY(51,5);
LcdCharacter('t');
gotoXY(45,5);
LcdCharacter(' ');
gotoXY(39,5);
LcdCharacter('S');
gotoXY(33,5);
LcdCharacter('c');
gotoXY(27,5);
LcdCharacter('a');
gotoXY(21,5);
LcdCharacter('n');
// Read signals
for (unsigned char n=0; n<MAXCHANNEL; n++){
// Read the signal on current channel
unsigned char s = radio.RADIO_RSSI(n);
s = map(s, 0, 31, 0,Lcd_visible);
if(s < signal[n])
{
signal[n] = signal[n];
}
else {
signal[n]=s;
}
}
for (unsigned char n = LCD_VISIBLE_X_RES; n > 0; n--){
drawLine(n,Lcd_TOP,PIXEL_OFF);
drawLine(n,signal[abs(LCD_VISIBLE_X_RES-n)], PIXEL_ON);
}
}
else if (mode == 4)
{
gotoXY(75,5);
LcdCharacter('V');
gotoXY(69,5);
LcdCharacter('o');
gotoXY(63,5);
LcdCharacter('l');
gotoXY(57,5);
LcdCharacter('t');
gotoXY(51,5);
LcdCharacter('m');
gotoXY(45,5);
LcdCharacter('e');
gotoXY(39,5);
LcdCharacter('t');
gotoXY(33,5);
LcdCharacter('e');
gotoXY(27,5);
LcdCharacter('r');
gotoXY(21,5);
LcdCharacter('(');
gotoXY(15,5);
LcdCharacter('A');
gotoXY(9,5);
LcdCharacter('5');
gotoXY(3,5);
LcdCharacter(')');
int aread = analogRead(5);
float readval = (float)aread / 1023.0;
readval = readval * 5.0;
readval = readval * 100.0;
int finalval = int(readval);
char buffer [3] = {
0, 0, 0 };
itoa (finalval,buffer,10);
gotoXY(50,3);
LcdCharacter(buffer[0]);
gotoXY(44,3);
LcdCharacter('.');
gotoXY(38,3);
LcdCharacter(buffer[1]);
gotoXY(32,3);
LcdCharacter(buffer[2]);
delay(500);
}
else if (mode == 5)
{
toggled = 0;
gotoXY(75,5);
LcdCharacter('M');
gotoXY(69,5);
LcdCharacter('i');
gotoXY(63,5);
LcdCharacter('n');
gotoXY(57,5);
LcdCharacter('i');
gotoXY(51,5);
LcdCharacter('-');
gotoXY(45,5);
LcdCharacter('S');
gotoXY(39,5);
LcdCharacter('c');
gotoXY(33,5);
LcdCharacter('o');
gotoXY(27,5);
LcdCharacter('p');
gotoXY(21,5);
LcdCharacter('e');
gotoXY(15,5);
LcdCharacter(' ');
gotoXY(9,5);
LcdCharacter('A');
gotoXY(3,5);
LcdCharacter('5');
for (unsigned char n = LCD_VISIBLE_X_RES; n > 0; n--){
drawLine(n,Lcd_TOP,PIXEL_OFF);
int mappedval = map(analogRead(5), 0, 1023, 0,Lcd_visible);
drawLine(n,mappedval,PIXEL_ON);
}
delay(10);
}
}
void switchmode()
{
toggled = 0;
LcdClear();
if(mode == MODECOUNT)
{
mode = 1;
}
else
{
mode++;
}
}
void toggle()
{
toggled = 1;
}