Pages: [1]   Go Down
Author Topic: First step (SWR-meter).  (Read 1615 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all!

I am very new in programming and this is my first skatch. The goal was to build an SWR-meter. The signals will be read from ADC0 and ADC1, calculate SWR and Power and than displayed on LCD-display. Is any mistakes in my skatch?


Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
float forward;
float reflection;
float val;

void setup() {
   lcd.begin (16,2);
   }

void loop() {

forward = analogRead(0);
reflection = analogRead(1);
val = 0;
val = forward * 0.098;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Power=");
lcdPrintFloat(val, 1);
lcd.setCursor(5, 1);
lcd.print("SWR=");
val = 0;
val = ((forward + reflection) / (forward - reflection));
lcdPrintFloat(val, 1);

delay (100);

}

void lcdPrintFloat (float val, byte precision) {
  lcd.print ((long)val);
  if (precision > 0) {
    lcd.print (".");
    unsigned long frac;
   unsigned long mult = 1;
  byte padding = precision -1;
 while (precision--)
           mult *=10;
     if (val >= 0)
           frac = (val - int(val)) * mult;
      else frac = (int(val) - val) * mult;
 unsigned long frac1 = frac;
while( frac1 /= 10 )
        padding--;
while( padding-- )
        lcd.print("0");
lcd.print(frac,DEC);

  }}
 
 

Thanks!
« Last Edit: April 26, 2012, 12:39:42 pm by willi1 » Logged

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you edit your post and re-insert your code using code tags (the # button) so we can see your indentation correctly?
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: