Go Down

Topic: First step (SWR-meter). (Read 2117 times) previous topic - next topic

willi1

Apr 26, 2012, 06:41 pm Last Edit: Apr 26, 2012, 07:39 pm by willi1 Reason: 1
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: [Select]
#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!

MarkT

Can you edit your post and re-insert your code using code tags (the # button) so we can see your indentation correctly?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy