Compiling error and general questions.

I realise that this isnt complete yet, however I am having real trouble with this programming, I have used the examples pages but I am struggling to figure out my errors. Any help would be greatly appreciated.

The idea behind this is to read each of the 8 analog inputs, only one of them will have an input at a time, as theyre connected to plates on a slider. Once it determines which is high, it then runs the subroutine to calculate (10 bit) the voltage level.

Well, thats what its supposed to do :stuck_out_tongue:

int A0pin = 19;
int A1pin = 20; 
int A2pin = 21; 
int A3pin = 22; 
int A4pin = 23; 
int A5pin = 24; 
int A6pin = 25; 
int A7pin = 26; 
int analogValue;

void setup()
{
}

void loop()
int (analogValue = analogRead(A0pin));
if (analogValue < 100) {
  Calc();
}
int (analogValue = analogRead(A1pin));
if (analogValue < 100) {
  Calc();
}
int (analogValue = analogRead(A2pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A3pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A4pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A5pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A6pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A7pin));
if (analogValue < 100) {
  Calc();
}



void Calc()
{ 

}

sketch_feb25a:18: error: expected initializer before ‘int’
sketch_feb25a:19: error: expected unqualified-id before ‘if’
sketch_feb25a:22: error: expected )' before '=' token sketch_feb25a:23: error: expected unqualified-id before 'if' sketch_feb25a:26: error: expected )’ before ‘=’ token
sketch_feb25a:27: error: expected unqualified-id before ‘if’
sketch_feb25a:31: error: expected )' before '=' token sketch_feb25a:32: error: expected unqualified-id before 'if' sketch_feb25a:36: error: expected )’ before ‘=’ token
sketch_feb25a:37: error: expected unqualified-id before ‘if’
sketch_feb25a:41: error: expected )' before '=' token sketch_feb25a:42: error: expected unqualified-id before 'if' sketch_feb25a:46: error: expected )’ before ‘=’ token
sketch_feb25a:47: error: expected unqualified-id before ‘if’
sketch_feb25a:51: error: expected `)’ before ‘=’ token
sketch_feb25a:52: error: expected unqualified-id before ‘if’

You have no opening and closing bracers in your loop() function.

int (analogValue = analogRead(A0pin));
if (analogValue < 100) {
  Calc();
}

What’s that?

h4nsn:
You have no opening and closing bracers in your loop() function.

Damn, I cant believe I overlooked that, I spent ages trying to figure that out. Putting that in corrected all the other errors too!

AWOL:

int (analogValue = analogRead(A0pin));

if (analogValue < 100) {
 Calc();
}



What's that?

That is supposed to check analogue vlavue at A0, and store it in ‘analogValue’ - If the value is more than 100 then its the active analog input and it should run the subroutine Calc().
If the value is less than 100, it moves to the next analog input, and checks that etc.

Note - I have my < and > mixed up, corrected them now.

I havent written the calc yet, Im just working as I go, writing it in stages so I dont get the end and its all wrong. :slight_smile:

By the way your code could be reduced to just a couple of lines.
Whenever you find yourself repeating a lot of code a function or loop is probably a better alternative.

loop()
{
   int i;
   for (i = 19; i < 27; ++i) {
       if (analogRead(i) > 100)
            calc(i);
   }
}

Very nice, thank you.

Thats a much simpler way of doing this! So with Arduinos, it just knows that 19 incremented to 27 means the pin numbers, and not just an integer number?

When you define your pin numbers with

int A0pin = 19;

A0pin is just a variable containing an integer, 19.
Therefore

analogRead(A0pin)
// and
analogRead(19);

are totally equivalent.

I'm pretty new to the arduino thing myself, but arent analog inputs written like 'A0, A1, A2..' etc? I.E not just normal integers '1, 2, 3...'

I'm pretty new to the arduino thing myself, but arent analog inputs written like 'A0, A1, A2..' etc? I.E not just normal integers '1, 2, 3...'

this is what was confusing me, in some of the examples, they have things like that "int Sensor = 19 // sensor is pin 19". I took this to mean that they were using this to set pin numbers to a word.

Really appreciate your feedback my friend. Im going to get started now on figuring out how to get my subroutine to determine which 1/8 of the voltage scale its in! 0-1023 is the standard 10 bit resolution.

128, 256, 384, 512,640, 768, 896, 1023!

you missed {} after void loop()
Do like this

void loop()
{
int (analogValue = analogRead(A0pin));
if (analogValue < 100) {
  Calc();
}
int (analogValue = analogRead(A1pin));
if (analogValue < 100) {
  Calc();
}
int (analogValue = analogRead(A2pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A3pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A4pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A5pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A6pin));
if (analogValue < 100) {
  Calc();
}

int (analogValue = analogRead(A7pin));
if (analogValue < 100) {
  Calc();
}

}

Ok ive made some progress throughout today. I am at the stage now where I am stuck again.

I am under the impression that so far, this code checks 8 analog inputs in turn, searching for the one with an input, it takes the value of the analog voltage and uses it in the “voltage” function, to determine which 1/8 of the voltage band it is in.
once determined, its moves to the conversion function, which turn both the 1-8 analog options, and the 1-8 voltage levels, and converts them into a single binary ranging from 00000000 upto 01110111. Basically giving a reading of 1-64 in binary.

I now need to convert this back to decimal so that I can continue to use it in further calculations. Any help here, and feedback on the whole coding would be very much appreciated.

As can be seen in the attachment, its basically just trying to find the location of the slider to a 1/64 resolution. 8 large plates with 8 possible voltages.

int i;  // large plate
int x;
int a;  // small platelet
int b;  // large plate


void setup()
{
}


void loop()
{
  for (i = 19; i < 27; ++i) {
    if (analogRead(i) > 100)
      voltage(i);
  }
}

int voltage(int x)
{ 
  float voltage = x * (5.0 /1023.0);
  if (voltage > 4.3515)
    convert(8);
  if (voltage < 4.3515) && (voltage > 3.7305)
    convert(7);
  if (voltage < 3.7305) && (voltage > 3.1105)
    convert(6);
  if (voltage < 3.1105) && (voltage > 2.4915)
    convert(5);
  if (voltage < 2.4915) && (voltage > 1.874)
    convert(4);
  if (voltage < 1.874) && (voltage > 1.2595)
    convert(3);
  if (voltage < 1.2595) && (voltage > 0.6535)
    convert(2);
  if (voltage < 0.6535)
    covert(1);
}

int convert(int w)
{  
  int myNum = w;
  int zeros = 8 - String(myNum,BIN).length();
  String platelet;
  for (int i=0; i<zeros; i++) {
    platelet = platelet + "0";
  }
  platelet = platelet + String(myNum,BIN);         

  int MyNum = i;
  int zeros = 8 - String(MyNum,BIN).length();
  String plate;
  for (int i=0; i<zeros; i++) {
    plate = plate + "0";
  }
  plate = plate + String(MyNum,BIN);         

  platelet = a;
  plate = b;
  calc(a, b)
}



int calc(int y, int z)
{
  uint8_t lower_part = y;
  uint8_t upper_part = z;
  uint8_t result = 0; // accumulator to build result
  result |= (lower_part & 0b00001111); // i
  result |= ((upper_part & 0x00001111) << 4); // 

}
// "print" result = 0b 0011 0101

  for (i = 19; i < 27; ++i) {
    if (analogRead(i) > 100)
      voltage(i);
  }

When you’re calling ‘voltage’ you’re actually passing it a pin number, not the value you got from analogRead. You want something like:

int volts;
[...]
  for (i = 19; i < 27; ++i) {
    if ((volts = analogRead(i)) > 100)
      voltage(volts);
  }

That’s a start at least.

Regards,

Brad
KF7FER

I spotted this and corrected in my actual program, although yours is much tidier, thank you!

krugerr:
I spotted this and corrected in my actual program, although yours is much tidier, thank you!

Hmmm… well I see you had some compile errors though perhaps you’ve fixed those as well?

I think this looks better (sorry I don’t know if it really works or not)

int voltage(int x)
{ 
  float volts = x * (5.0 /1023.0);

  if (volts < 0.6535)
    convert(1);
  else if (volts < 1.2595)
    convert(2);
  else if (volts < 1.874)
    convert(3);
  else if (volts < 2.4915)
    convert(4);
  else if (volts < 3.1105)
    convert(5);
  else if (volts < 3.7305)
    convert(6);
  else if (volts < 4.3515)
    convert(7);
  else
    convert(8);
    
  return (int)volts;
}

You also seemed to define zero’s twice in your “convert” function so perhaps that’s just a cut and paste problem?

Hope this helps,

Brad.

krugerr:
Any help here, and feedback on the whole coding would be very much appreciated.

I'm a bit slow today so I'm not going to be much help on solving your problem, but from looking at your code a few general thoughts come to mind:

  • Variable Names: Never use one character names for globals - it's always better to use something descriptive so it helps the reader - and you later - understand the code.
  • Strings: Just say no. Use an array of characters and functions such as itoa and strlen (convert ints to strings, returns the length of a string
  • If you don't know how arrays work, you should learn how and reduce the size of your code
  • I think having functions (which you ignore the return values of) for voltage, convert, and calc is a bit much - you should only need one routine plus loop and then only to keep the size of your main loop smaller
  • Again I'm slow but I don't see the point of calling convert and losing the voltage value

I hope that helps a bit.

Hey - i am currently reading up on Arrays, although Im not sure how to implement this into my code, yet :stuck_out_tongue:
currently the functions calculate different aspects of the problem, then, when I get around to it, hopefully use all those solutions to calculate the exact location of the pin on the slider.

Can I ask why we should avoid string use? I am investigating array use however :slight_smile:

int convert(int w)
{  
  int myNum = w;
  int zeros = 8 - String(myNum,BIN).length();
  String platelet;
  for (int i=0; i<zeros; i++) {
    platelet = platelet + "0";
  }
  platelet = platelet + String(myNum,BIN);         

  int MyNum = i;
  int zero = 8 - String(MyNum,BIN).length();
  String plate;
  for (int i=0; i<zero; i++) {
    plate = plate + "0";
  }
  plate = plate + String(MyNum,BIN); 
  
int Plate = plate.toInt();  
int Platelet = platelet.toInt(); 

  calc(Platelet, Plate);
  }



  int calc(int y, int z)
  {
    uint8_t lower_part = y;
    uint8_t upper_part = z;
    uint8_t result = 0; // accumulator to build result
    result |= (lower_part & 0b00001111); // i
    result |= ((upper_part & 0x00001111) << 4); // 

  }

  }

Int w and int i - these are values of 1-8 and are from further up in the, this function is supposed to turn them both into binary, and shift one of them up so that a reading 64 different combinations are availble.

Is this correct so far?

krugerr:
Can I ask why we should avoid string use? I am investigating array use however :slight_smile:

There is a big difference between a string (c-style character array) and a String data type. Spend a few days reading the forums and it comes up every couple of days (or check out http://forum.arduino.cc/index.php/topic,112067.0.html for an decent discussion).

Regards,

Brad.

good thread - thank you for sharing that one. Im going to try switching it around then instead :slight_smile: