Aother newb question

I am TOTALY new to Arduino, microcontolers and programming so let me start out by apologizing for any obvious newbie questions I am SURELY going to ask

I want to replace a standard meter movement in an antenna rotatator control box with an LCD that shows compass heading in degrees....this is acheived by reading a voltage from a pot in the rotor through a voltage divider to limit it to 0-5v I think I have all that figured and fudged up a sketch using peices of code from examples but I thought I could augment it a little and show North south east and west in the display as well but I am not sure how to go about this

I am sure it will have to use if and ifelse statements but brain is quickly turning to jello for instance beam heading 170 - 190 degrees display S beside the degree readout and the same for all the directions

I hope I explained this adaquetly I will include the simple sketch I have so far

/*
CDE bearing
Reads an analog input on pin 0, converts it to a compass bearing, and prints the result to the serial monitor and LCD
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("T. Vanwort VE3MQ");
}

// the loop routine runs over and over again forever with a 1 sec delay:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (360 / 1023.0);
// print out the value you read:
Serial.println(voltage);
lcd.setCursor(5, 1);
lcd.print(voltage);
delay(500);
}

I would recommend using SwitchCase rather than a long string of if statements. I find it produces less brain jello than long strings of if and if/else statements.

http://arduino.cc/en/Reference/SwitchCase

If there are preferred heading in degrees, you can place them at the top of the switch case list, and gain some efficiency because the other conditions wont be checked for once the direction is matched..

maybe a good idea to use a title that matches the real question :slight_smile:

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (360 / 1023.0);

Nonsense. The nominal voltage on an Arduino is NOT 360 degrees.

If there is a linear relationship between angular position and sensorValue, that relationship is NOT voltage. So, pick a better name.

If there is a linear relationship between the sensor value and angular position, then the next step is to define that range of values that represent the names that you want to display. The first step in this process is to determine how many names you want. Just N, S, E, and W? or N, NE, E, SE, S, SW, W, and NW? Determine, then, the minimum and maximum values for each name, and a series of if/else if statements involving the ranges and names.

you could try map function (if as PaulS mentioned there is linearity on your sensor) and a simple array.

this is set up for a pot, but you get the idea... most TV antennas cannot rotate past 360 degrees so it may work for you.

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int myDirection [8] = {23, 68, 113, 158, 203, 248, 293, 338};
String NSEW[8] = {"North", "NorthEast", "East", "SouthEast", "South", "SouthWest", "West", "NorthWest"}; 
String myWord;
//
void setup() 
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("T. Vanwort VE3MQ");
}
//
void loop() 
{
  //int reading = 359;
  int reading = map(analogRead(A0), 0, 1023, 0, 360);
  for (int i = 0; i < 8; i++)
  {
    if (reading <= myDirection[i])
    {
      myWord = NSEW[i];
      break;
    }
    else
    myWord = NSEW[0];
  }
  Serial.println(myWord);
  Serial.print(reading);
  lcd.clear();
  lcd.setCursor(5, 1);
  lcd.print(myWord);
  delay(1000);
}

You may have to wait a few weeks, but Arduino Projects for Amateur Radio (see Amazon.com) has exactly what you are looking for. The actual circuit varies according to the type of rotator you have, too. Most have 0-360 degree rotation, but the Yaesu has 0 - 450.

Jack, W8TEE

econjack:
Most have 0-360 degree rotation, but the Yaesu has 0 - 450.

in that case, you could simply use the modulo function:

void loop() 
{
  //int reading = 361;
  int reading = map(analogRead(A0), 0, 1023, 0, 450);
  reading = reading % 360;
  for (int i = 0; i < 8; i++)
  {
    if (reading <= myDirection[i])
    {
      myWord = NSEW[i];
      break;
    }
    else
    myWord = NSEW[0];
  }
  Serial.println(myWord);
  Serial.println(reading);
  lcd.clear();
  lcd.setCursor(5, 1);
  lcd.print(myWord);
  delay(1000);
}

Thanks for all the input as for the inaccuracies in the comments I agree and the final sketch will reflect what ir actually happening , this is just a rough draft and is my first attempt at an actual working program , the only other thing I tried was "Blink" and "Hello World!" LOL Being able to see in a real life application goes a long way to help in understanding how the programming functions work in real life