If statement not working

Hi, I am using a Nano and a AD9833. I am trying to set the frequency output of the 9833 according to what digital input pin is high. I can set successfully the inital frequency by changing the "long Freq" declaration, but none of the if statements ever evaluate to High. I have verified that I have 5v going to the pin, and also a pull down resistor. I can't get the if statement to evaluate true no matter what I do.

Here is my code:

#include <Wire.h> //for I2C
#include <MD_AD9833.h> //for Frequency Generator

// Pins for SPI comm with the AD9833 IC
#define DATA 11 ///< SPI Data pin number
#define CLK 13 ///< SPI Clock pin number
#define FSYNC 10 ///< SPI Load pin number
MD_AD9833 AD(FSYNC); // Hardware SPI

// the regular (waveform select) button:
int freq1=2; // for selecting 1000 Hz
int freq2=3; // for selecting 2000 Hz 

long Freq = 1000; // starting frequency

void setup() {

Serial.begin(9600); // open the serial port at 9600 bps:

pinMode(freq1, INPUT);    // sets the digital pin as input
pinMode(freq2, INPUT);    // sets the digital pin as input

//go ahead and set the frequency to whatever is currently selected
if (freq1 == HIGH) {
  //set freq to 1000HZ
  Freq = 1000;
}else if (freq2 == HIGH) {   //add next frequency here
  //set freq to 2000HZ
Freq = 2000;
}

AD.begin(); // start the generator board
delay(1000);
AD.setMode(MD_AD9833::MODE_SINE);
AD.setFrequency(MD_AD9833::CHAN_0, Freq);
}

void loop() {
  
if (freq1 == HIGH) {    // read the input pin
  //set freq to 1000HZ
Freq = 1000;
} else if (freq2 == HIGH) {   //add next frequency here
  //set freq to 2000HZ
Freq = 2000;
}

AD.setFrequency(MD_AD9833::CHAN_0, Freq);

delay(500);
}

freq1 was defined as int type and HIGH is Boolean value. No work.

May be :

if (digitalRead(freq1) == HIGH)

Same problem.

2 Likes

Welcome to the forum

Shouldn't you be testing the state of the input pins using DigitalRead() instead of testing the pin numbers ?

1 Like

Wow, bro, I'm an idiot. I didn't use digital read. I changed it to...

if (digitalRead(freq1 == HIGH)) {

Your post triggered that in my mind, lol. It's been a long day. Imma hang it up till tomorrow.

Thanks!

  • No

if (digitalRead(freq1) == HIGH) {

4 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.