TEA5767 Spirit box (Ghost Box)

Hello,

I’ve been playing about with my Arduino UNO and woukd like to ask some questions regarding the code I already have.

I am make a spirit box for use when we hold an event. I have already managed to get the frequencies for the radio and I have already got it so that it sweeps in forward/reverse/random.

My questions are about ways to call the sweep functions. I have tried with push buttons and while that seems to function pressing button A starts the sweep forward but pressing Button B does nothing until the previous function has finished, then pressing Button B starts the sweep in reverse. What I would like to happen is when Button A is pressed it starts the forward sweep but then if Button B is pressed that function is terminated and the Reverse sweep function is initiated.

Is this at all possible? I have toyed with the idea of using a toggle switch but thought about using buttons to start with.

Another I would like to know is that when Button A is pressed and it sweeps through the frequencies 87.0 to 108.9 is there any way to Start it again without having to press the button again? Sort of having that function on a continuous loop until one of the other buttons is pressed.

Any help would be appreciated. I know from my reading around on here that there are a few others that have been trying to make one of these projects so maybe they have some input.

Here follows my code:

//Visage Paranormal LTD - Spirit Box v1.0

#include <LiquidCrystal.h>
#include <Wire.h>


unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 0;

int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int buttonApin = 9;
int buttonBpin = 10;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()
{
  Wire.begin();
  frequency = 87.0; //starting frequency
  setFrequency();
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);  
  lcd.begin(16, 2);
  lcd.setCursor(7, 0);
  Serial.begin(9600);
}

void loop()
{
  if (digitalRead(buttonApin) == LOW)
  {
    ForwardSweep(); //this calls the forward sweep function when Button A is pressed.
  }
  if (digitalRead(buttonBpin) == LOW)
  {
    ReverseSweep(); //this calls the reverse sweep function when Button B is pressed.
  }  
}

/*this is for a random frequency sweep*/

/*void RandomSweep()
/*RANDOM FREQUENCY SWEEP
{
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    delay(100); 
  for ( int i = 870 ; i < 1090 ; i++ ) {
  frequency = random(870,1090) / 10.0;
  setFrequency();
  lcd.setCursor(4, 0);
  lcd.print(frequency);
  lcd.print(" MHz ");
  Serial.println(frequency);            //used for testing without the LCD screen
}
}*/

/*this part sweeps forward through the frequencies 87.0 to 108.9*/

void ForwardSweep()
/*FORWARD SWEEP*/
{
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    delay(100); 
  for ( int i = 870 ; i < 1090 ; i++ ) {
  float frequency = i/10.0 ;
  setFrequency();
  lcd.setCursor(4, 0);
  lcd.print(frequency);
  lcd.print(" MHz ");
  Serial.println(frequency);         //used for testing without the LCD Screen
}
}

/*this part is for sweeping in reverse from 108.9 to 87.0*/

void ReverseSweep()
/*REVERSE SWEEP*/
{
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    delay(100); 
  for ( int i = 1090 ; i >= 870 ; i-- ) {
  float frequency = i/10.0 ;
  setFrequency();
  lcd.setCursor(4, 0);
  lcd.print(frequency);
  lcd.print(" MHz ");
  Serial.println(frequency);         //used for testing without the LCD screen
}
}

/*this part is for the sweep speed controlled by 10k pot*/

void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  lcd.setCursor(1, 0);
  lcd.print("SR: ");
  lcd.setCursor(1, 5);
  lcd.print(val);
    Serial.println(val); // just to check sweep rate without the LCD screen attached
  delay(val);
 
}

It is possible. The best way would be to quit using delay(...) and learn to use millis(). There is a tutorial which is a locked post at the top of this forum.

Another way would be to check the buttons each time you make a pass through each of your for loops.

Any time you want to intercept a button push, unless it's wired in to an interrupt, you need to poll the attached pin looking for the event to occur. In each of your forward/reverse sweep functions you need to include a check to see if the other button has been pushed.

Interrupts can be difficult for a beginner. Polling instead of using interrupts may be much easier for you.

Hi All,

Thanks for your responses. Much Appreciated!

One thing I am noticing is that when I press Button A it does start the forward sweep but when it gets to the end it then moves onto the reverse sweep without me even pressing the second button.

I don't know what I have done wrong. I thought it should just run through the called function once and that was it.

Thanks,

Brett

Hi Everyone,

Quick update!

I now have it switching between modes Forward Sweep / Reverse Sweep and Vice Versa.

#include <Button.h>

#include <LiquidCrystal.h>
#include <Wire.h>


unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 0;
int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//each button needs to be declared
Button* forwardButton;
Button* reverseButton2;


void setup() {
  Wire.begin();
  frequency = 87.0; //starting frequency
  setFrequency();
  lcd.begin(16, 2);
  lcd.setCursor(7, 0);
  Serial.begin(9600);
    //create a button on pin 12. We invert the logic (invert=true)
  //so that even we are in input pullup mode, a press to the button
  //will be detected as a "on" value and a rising edge.
  forwardButton = new Button(11, INPUT_PULLUP, true);
  reverseButton2 = new Button(10, INPUT_PULLUP, true);
  //attach funtion to be triggered on rise or fall
  forwardButton->attachFunction(&ForwardSweep, RISING);
  reverseButton2->attachFunction(&ReverseSweep, RISING);

  
}

void loop() {
  //this update the status of the button and must be called at every loop
  forwardButton->poll();
  reverseButton2->poll();
}

void ForwardSweep()
//FORWARD SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  for ( int i = 870 ; i < 1000 ; i++ ) { //value changed to 1000 for testing should be 1090
    float frequency = i / 10.0 ;
    setFrequency();
    lcd.setCursor(4, 0);
    lcd.print("Forward Sweep");
    lcd.setCursor(4, 1);
    lcd.print(frequency);
    lcd.print(" MHz ");
    Serial.println("Forward Sweep");
    Serial.println(frequency);         //used for testing without the LCD Screen
    reverseButton2->poll();
    forwardButton->poll();
  }
}

void ReverseSweep()
//REVERSE SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  for ( int i = 1090 ; i >= 870 ; i-- ) {
    float frequency = i / 10.0;
    setFrequency();
    lcd.setCursor(4, 0);
    lcd.print(frequency);
    lcd.print(" MHz ");
    Serial.println("Reverse Sweep");
    Serial.println(frequency);         //used for testing without the LCD screen
    forwardButton->poll();
    reverseButton2->poll();
  }
}

void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  lcd.setCursor(1, 0);
  lcd.print("SR: ");
  lcd.setCursor(1, 5);
  lcd.print(val);
  Serial.println(val); // just to check sweep rate without the LCD screen attached
  delay(val);

}

Above code is based on the interruptStyle example that comes with the button library.

My next question may be something very simple indeed. When I have selected a sweep (either forward or reverse) how do I get it to keep looping until another button is pressed. At the moment it is only going through the function once. Is there a way to make the function call itself and re-run it over again until another button is pressed to change the mode.

Thanks for all your help!

If you ditched the Button class, and used the state change detection example (with a tiny delay for debouncing), it would be extremely obvious how to start a sweep in one direction when one switch becomes pressed, and how to start a sweep in the other direction when the other switch becomes pressed.

I have gone to the State Change Detection example and have worked through it as advised by Paul S.

I can start the sweep in one direction but am having problems. When I press the other button to sweep in the other direction nothing happens. Can anybody please help me out. It’s driving me nuts!

Basically, press the forward button to start the sweep. If i want to change the sweep direction half way through I want to be able to press the other button which will stop the previous function and start the function for the reverse sweep. Up to now All i have is the sweep in one direction. Only when I reset the Arduino will it allow me to press the other button to go in the opposite direction.

Here my code, what do you all think is wrong with it?

#include <LiquidCrystal.h>
#include <Wire.h>


// this constant won't change:
const int buttonPin = 10;     // the pin that the forward sweep button is attached to
const int buttonPin2 = 11;    // the pin that the reverse sweep button is attached to

// Variables will change:
int buttonPushCounter = 0;    // counter for the number of forward sweep button presses
int buttonCurrState = 0;      // current state of the forward sweep button
int PrevButtonState = 0;      // previous state of the forward sweep button
int buttonPushCounter2 = 0;   // counter for the number of Reverse sweep button presses
int buttonCurrState2 = 0;     // current state of the Reverse sweep button
int PrevButtonState2 = 0;     // previous state of the Reverse sweep button
unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 0;
int potpin = A0;              // analog pin used to connect the potentiometer to adjust the sweep speed
int val;                      // variable to read the value from the analog pin

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // initialize the button pins as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  // initialize serial communication:
  Serial.begin(9600);
  Wire.begin();
  frequency = 87.0; //starting frequency
  setFrequency();
  lcd.begin(16, 2);
  lcd.setCursor(7, 0);
}


void loop() {
  // read the forward sweep button input pin:
  buttonCurrState = digitalRead(buttonPin);
  // compare the forward button current state to its previous state
  if (buttonCurrState != PrevButtonState) {
    // if the state has changed, increment the counter
    if (buttonCurrState == LOW) {
      // if the current state is LOW then the button went from off to on:
      buttonPushCounter++;
    } else {
      // DO NOTHING
    }
  }
  // read the reverse sweep button input pin
  buttonCurrState2 = digitalRead(buttonPin2);
  //compare the reverse button current state with it's previous state
  if (buttonCurrState2 != PrevButtonState2) {
    // if the state has changed, increment the counter
    if (buttonCurrState2 == LOW) {
      // if the current state is LOW then the button went from off to on:
      buttonPushCounter2++;
    } else {
      // Do Nothing
    }
  }
  // Delay a little bit to avoid bouncing
  delay(50);
  // save the current state as the last state, for next time through the loop
  PrevButtonState = buttonCurrState;
  PrevButtonState2 = buttonCurrState2;

  if (buttonPushCounter == 1) {
    Serial.println("Forward Sweep");
    ForwardSweep(); //Run the Forward Sweep Function
  } else {
  }

  if (buttonPushCounter2 == 1) {
    Serial.println("Reverse Sweep");
    ReverseSweep(); // Run the Reverse Sweep Function
  } else {
  }
}

void ForwardSweep()
//FORWARD SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  digitalRead(buttonPin);
  for ( int i = 870 ; i < 900 ; i++ )
  {
    float frequency = i / 10.0 ;
    setFrequency();
    //lcd.setCursor(4, 0);             LCD not used while debugging
    //lcd.print("Forward Sweep");      LCD not used while debugging
    //lcd.setCursor(4, 1);             LCD not used while debugging
    //lcd.print(frequency);            LCD not used while debugging
    //lcd.print(" MHz ");              LCD not used while debugging
    Serial.println(frequency);         //used for testing without the LCD Screen
  }
}

void ReverseSweep()
//REVERSE SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  for ( int i = 900 ; i >= 870 ; i-- )
  {
    float frequency = i / 10.0;
    setFrequency();
    //lcd.setCursor(4, 0);              LCD not used while debugging
    //lcd.print(frequency);             LCD not used while debugging
    //lcd.print(" MHz ");               LCD not used while debugging
    Serial.println(frequency);         //used for testing without the LCD screen
  }
}

void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  val = analogRead(potpin);           // reads the value of the potentiometer (Sweep Speed) (value between 0 and 1023)
  map (val, 0, 1023, 0, 100);         // Maps the values of the potentiometer (Sweep Speed) to 0 and 100
  int PercentageAct = map(val, 0, 1023, 0, 10000);
  int PercentageActRound = ((PercentageAct / 100)); //Convert Potentiometer (Sweep Speed) value to % and Round for Display
  Serial.print(float(PercentageActRound)); //Display Sweep speed as a percentage without LCD attached
  Serial.println(" % ");
  //lcd.setCursor(1, 0);             LCD not used while debugging
  //lcd.print("SR: ");               LCD not used while debugging
  //lcd.setCursor(1, 5);             LCD not used while debugging
  //lcd.print(val);                  LCD not used while debugging
  delay(val);
}
const int buttonPin = 10;     // the pin that the forward sweep button is attached to
const int buttonPin2 = 11;    // the pin that the reverse sweep button is attached to

Wouldn't forwardPin and reversePin make more sense?

int buttonCurrState = 0;      // current state of the forward sweep button
int PrevButtonState = 0;      // previous state of the forward sweep button

Why is Curr in the middle and Prev at the beginning? It makes more sense to me to name the pins currForwardState and prevForwardState.

The state change detection example showed counting switch presses, so you could have different actions based on how many times the switch was pressed. Since the count is irrelevant to you, eliminate all the crap involved with counting. Simply set a boolean, sweepForward, to true when the forward switch becomes pressed and to false when the reverse switch becomes pressed. Have another boolean variable, sweepReverse, set the opposite ways.

Call the relevant functions if sweepForward is true or if sweepReverse is true.

You have no Serial.print() statements in those functions that indicate which function you are in. Why not?

How do you KNOW that ReverseSweep() is even called? How do you KNOW that it is not doing what you expect?

I know it's not working right because when I press the forward sweep button I can see on the serial monitor that it says forward sweep as per this code:

if (buttonPushCounter == 1) {
    Serial.println("Forward Sweep");
    ForwardSweep(); //Run the Forward Sweep Function
  } else {
  }

and also I can see it printing the frequencies in the serial monitor using this code in the function ForwardSweep():

    Serial.println(frequency);         //used for testing without the LCD Screen

I will give what you suggest a try and see if I can get it working. Thanks for the advice Paul!

Hi Paul.

Changed everything you suggested. Thanks, it does make more sense now! However it’s still not working as expected. Do I need to put anything else in there at all?

#include <LiquidCrystal.h>
#include <Wire.h>


// this constant won't change:
const int forwardPin = 10;     // the pin that the forward sweep button is attached to
const int reversePin = 11;    // the pin that the reverse sweep button is attached to

// Variables will change:
bool SweepForward = false;        // Variable to store if forward sweep button is pressed
int CurrForwardState = 0;      // current state of the forward sweep button
int PrevForwardState = 0;      // previous state of the forward sweep button
bool SweepReverse = false;        // Variable to store if the reverse sweep button is pressed
int CurrReverseState = 0;     // current state of the Reverse sweep button
int PrevReverseState = 0;     // previous state of the Reverse sweep button
unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 0;
int potpin = A0;              // analog pin used to connect the potentiometer to adjust the sweep speed
int val;                      // variable to read the value from the analog pin

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // initialize the button pins as a input:
  pinMode(forwardPin, INPUT_PULLUP);
  pinMode(reversePin, INPUT_PULLUP);
  // initialize serial communication:
  Serial.begin(9600);
  Wire.begin();
  frequency = 87.0; //starting frequency
  setFrequency();
  lcd.begin(16, 2);
  lcd.setCursor(7, 0);
}


void loop() {
  // read the forward sweep button input pin:
  CurrForwardState = digitalRead(forwardPin);
  // compare the forward button current state to its previous state
  if (CurrForwardState != PrevForwardState) {
    // if the state has changed, increment the counter
    if (CurrForwardState == LOW) {
      // if the current state is LOW then the button went from off to on:
      //Serial.println(SweepForward);
      SweepForward = true;
      SweepReverse = false;
    } else {
      //DO NOTHING
    }
  }
  // read the reverse sweep button input pin
  CurrReverseState = digitalRead(reversePin);
  //compare the reverse button current state with it's previous state
  if (CurrReverseState != PrevReverseState) {
    // if the state has changed, increment the counter
    if (CurrReverseState == LOW) {
      // if the current state is LOW then the button went from off to on:
      SweepReverse = true;
      SweepForward = false;
      //Serial.println(SweepReverse);
    } else {
      //DO NOTHING
    }
  }
  // Delay a little bit to avoid bouncing
  delay(50);
  // save the current state as the last state, for next time through the loop
  PrevForwardState = CurrForwardState;
  PrevReverseState = CurrReverseState;

  if (SweepForward == true) {
    Serial.println("Forward Sweep");
    ForwardSweep(); //Run the Forward Sweep Function
    SweepReverse = false;
  } else {
    if (SweepReverse == true) {
      Serial.println("Reverse Sweep");
      ReverseSweep(); // Run the Reverse Sweep Function
      SweepForward = false;
    }
  }
}

void ForwardSweep()
//FORWARD SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  digitalRead(reversePin);
  for ( int i = 870 ; i < 900 ; i++ )
  {
    float frequency = i / 10.0 ;
    setFrequency();
    //lcd.setCursor(4, 0);             LCD not used while debugging
    //lcd.print("Forward Sweep");      LCD not used while debugging
    //lcd.setCursor(4, 1);             LCD not used while debugging
    //lcd.print(frequency);            LCD not used while debugging
    //lcd.print(" MHz ");              LCD not used while debugging
    Serial.println(frequency);         //used for testing without the LCD Screen
  }
}

void ReverseSweep()
//REVERSE SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  for ( int i = 900 ; i >= 870 ; i-- )
  {
    float frequency = i / 10.0;
    setFrequency();
    //lcd.setCursor(4, 0);              LCD not used while debugging
    //lcd.print(frequency);             LCD not used while debugging
    //lcd.print(" MHz ");               LCD not used while debugging
    Serial.println(frequency);         //used for testing without the LCD screen
  }
}

void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  val = analogRead(potpin);           // reads the value of the potentiometer (Sweep Speed) (value between 0 and 1023)
  map (val, 0, 1023, 0, 100);         // Maps the values of the potentiometer (Sweep Speed) to 0 and 100
  int PercentageAct = map(val, 0, 1023, 0, 10000);
  int PercentageActRound = ((PercentageAct / 100)); //Convert Potentiometer (Sweep Speed) value to % and Round for Display
  //Serial.print(float(PercentageActRound)); //Display Sweep speed as a percentage without LCD attached
  //Serial.println(" % ");
  //lcd.setCursor(1, 0);             LCD not used while debugging
  //lcd.print("SR: ");               LCD not used while debugging
  //lcd.setCursor(1, 5);             LCD not used while debugging
  //lcd.print(val);                  LCD not used while debugging
  delay(val);
}

However it's still not working as expected.

It does something. You expect it to do something. That's two things you haven't described.

Your code does look a lot better now.

 else {
      //DO NOTHING
    }

It is NOT necessary to have an empty else block. If there is nothing to do, don't have an else statement.

  // save the current state as the last state, for next time through the loop
  PrevForwardState = CurrForwardState;

I'd put this up with the forward pin reading code. But, that's just personal preference.

  } else {
    if (SweepReverse == true) {
      Serial.println("Reverse Sweep");
      ReverseSweep(); // Run the Reverse Sweep Function
      SweepForward = false;
    }
  }

Rather than being in the else block, this code should be at the same level as the if statement before it.

Only one body should be executed, but I'd do both tests at the same level.

What do your serial print's tell you (if you uncomment them)? Is the reverse switch being recognized the same as the forward switch?

Thanks Paul. I've restructured it as suggested.

The whole thing works to a degree. Yes when I press a button the related function does begin and run through in a loop. However, when I have the set the function running and I press the other button nothing happens. It is not starting the other function.

The serial monitor shows when the button is pressed as either 'Forward Sweep' or 'Reverse Sweep' is displayed then the loop through the frequencies begins again either forwards or backwards depending which button was pressed. What I expect to happen is that when I start the sweep in one direction I am able to press the other button and have it start the function to go in the other direction.

I guess this is what I expect to happen:

Press Button -> Start Forward/Reverse->Press other button->stop previous function->start new function in the other direction.

Basically I want to have 3 modes that can be selected Forward, Reverse and Random (forgetting Random until I can sort out Forward and Reverse). At the moment I can select forward or reverse but can't change it half way through if I wanted to.

I am just starting to sort out the Forward and Reverse sweep, when I have this working properly I want to add another button that will start a random sweep of the frequencies.

Your SweepForward() method blocks until it has completed one sweep. If you want to be able to switch directions immediately, you'll need to rewrite the functions to not block. The loop() function already loops. Let it do ALL the looping.

SweepForward() and SweepReverse() will check, each time they are called, if it is time to sweep, and, if so, will move ONE step in the proper direction (or will jump back to the beginning).

Nary a for loop in sight, when you are done.

Which functions do you mean to rewrite? The ForwardSweep() and ReverseSweep() ones?

If you do what do you suggest as this is the only way I could find of getting the frequencies sent to the FM module (TEA5767). Although I don't have it connected up as yet I can still see what it's doing using the Serial Monitor.

Which functions do you mean to rewrite? The ForwardSweep() and ReverseSweep() ones?

Yes.

If you do what do you suggest as this is the only way I could find of getting the frequencies sent to the FM module (TEA5767).

The part of the ForwardSweep() function that tells the FM module what frequency to tune to is:

   setFrequency();

I've looked at your code again, and you have local variables named frequency in the two functions, and a global variable of the same name (but different type). Setting the local variables does nothing, because setFrequency() uses the global variable, which never changes.

I can't imagine how your code is working, for any definition of working.

SUCCESS!

Thanks for your help and Guidance Paul.

This does actually work as I wanted it to work. All I have done is add an if statement to the function to look for the button press and set the boolean flag to true and break out of the function.

void ForwardSweep()
//FORWARD SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  digitalRead(reversePin);
  for ( int i = 870 ; i < 900 ; i++ )
  {
    float frequency = i / 10.0 ;
    setFrequency();
    //lcd.setCursor(4, 0);             LCD not used while debugging
    //lcd.print("Forward Sweep");      LCD not used while debugging
    //lcd.setCursor(4, 1);             LCD not used while debugging
    //lcd.print(frequency);            LCD not used while debugging
    //lcd.print(" MHz ");              LCD not used while debugging
    Serial.println(frequency);         //used for testing without the LCD Screen
    if (digitalRead(reversePin) == LOW) {
      SweepReverse = true;
      break;
    }
  }
}

void ReverseSweep()
//REVERSE SWEEP
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  delay(100);
  for ( int i = 900 ; i >= 870 ; i-- )
  {
    float frequency = i / 10.0;
    setFrequency();
    //lcd.setCursor(4, 0);              LCD not used while debugging
    //lcd.print(frequency);             LCD not used while debugging
    //lcd.print(" MHz ");               LCD not used while debugging
    Serial.println(frequency);         //used for testing without the LCD screen
    if (digitalRead(forwardPin) == LOW) {
      SweepForward = true;
      break;
    }
  }
}

You should put the code that is in loop(), that deals with reading the forward pin into a function, and call it from loop() and from ReverseSweep(). You should do the same for the code that deals with the reverse pin. You are not setting SweepReverse and SweepForward the same in the functions as in loop().

I still can't see where frequency every gets a new value, so I can't see that setFrequency() does anything useful.

is there any way possible u could share your correct code ive been searching everywhere and cant find the proper code im not smart enough to write code

1 Like

Hi,

Can you share the complete correct code with random sweep here with us? :slightly_smiling_face:
I’m a newbie of Arduino.