how to combine if and for loop?

Thread Starter

mussa

Joined Jun 10, 2019
45
I'm using Arduino uno, fm transmitter and LCD, I'm trying to change the frequency every minute or more. both for and if loop works alone.

C:
//#include <FMTX.h>
#include <Wire.h>

#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);
float fm_freq;

void setup() {
// put your setup code here, to run once:
  lcd.begin(16,2);
  lcd.backlight();

//  Serial.begin(9600);
//  Serial.print("FM-TX Demo\r\n");
//
//  fmtx_init(fm_freq, CHINA);
//
//  Serial.print("Channel:");
//  Serial.print(fm_freq, 1);
//  Serial.println("MHz");
}

void loop() {

  for(fm_freq=89.6;fm_freq<=108;fm_freq=fm_freq+0.2){

  delay(200);
  lcd.setCursor(0,0);
  lcd.print("FM frequncy");
  lcd.setCursor(0,1);
  lcd.print(fm_freq,1);//print the freq  on the lcd with
  }
  // put your main code here, to run repeatedly:
//
//  if(Serial.available()){
//    switch(Serial.read()){
//      case '&':
//        u8 i,buf[4];
//         float ch;
//         i=0;
//         delay(30);
//         while(Serial.available()&&i<4){
//           buf[I]=Serial.read();
//           if (buf[I]<= '9' && buf[I]>= '0') {
//           i++;}
//           else{
//           i=0;
//           break;
//           }
//         }
//         if (i==4){
//           ch = (buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0')*1+0.1*(buf[3]-'0');
//           if(ch>=70&&ch<=108){
//             Serial.print("New Channel:");
//             Serial.print(ch, 1);
//             Serial.println("MHz");
//             fmtx_set_freq(ch);
//           }else{
//             Serial.println("ERROR:Channel must be range from 88Mhz to 108Mhz.");
//           }
//         }else{
//           Serial.println("ERROR:Input Format Error.");
//         }
//        
//         while(Serial.available()){
//           Serial.read();
//         }
//        break;
//    }
//  }
}
Mod edit: code tags
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,415
One thing to avoid is leaving the main loop() function. Offhand I don't know what this will cause but my guess is your whole program will restart, meaning you run the setup() routing again and again. If nothing else this will make your display glitch.

Here is one way to do that:

CSS:
void loop() {
  while(1) {
    for(fm_freq=89.6;fm_freq<=108;fm_freq=fm_freq+0.2){
      delay(200);
      lcd.setCursor(0,0);
      lcd.print("FM frequncy");
      lcd.setCursor(0,1);
      lcd.print(fm_freq,1);//print the freq on the lcd with
    }
  }
}
Note that by wrapping the for loop within the while(1) infinite loop it will be run over and over.

Also note the use of "whitespace" (tabs) to better outline the code flow, along with the use of code tags (available on the icon bar above the text area you type in when replying).
 

nsaspook

Joined Aug 27, 2009
16,321
Seriously- I haven't actually used a flowchart since I took a programming class that used them.
Most of the time the flowchart is not for the original programmer, it's for the poor slob that years down the road is trying to make sense of your masterpiece.
 

djsfantasi

Joined Apr 11, 2010
9,237
“PseudoCode”!!!

Instead of coding right away and drawing a flowchart, design your program using short English phrases. It’s easy to understand while serving as a template for your code.
 
Top