Strange compiler syntax

Hi All,
Just trying to improve my C and looking at bit of code and programs, interested in doing many things besides playing at Robots. I can accross a bit of code to do with a frequency meter, but don't understand much of it, can't even see them in the Reference pages. and that is the use of :: 2 sets of colons as below? do they alter an internal timer or register? As an old Picaxer and very new Arduino fan I did notice that we have no COUNT input!

FreqCounter::f_comp= 8; // Set compensation to 12
FreqCounter::start(100); // Start counting with gatetime of 100ms
while (FreqCounter::f_ready == 0) // wait until counter ready

frq=FreqCounter::f_freq; // read result
Serial.println(frq); // print result
delay(20);

Can some one explain the workings to me, or better still point me in the right direction for a full listing of such bits of magic??

Regards
Mel.

Hello, :: is the scope operator, read this :wink:

http://www.cplusplus.com/doc/tutorial/classes/

:: isn't strange, it is the scope resolution operator (useful search term)

guix:
Hello, :: is the scope operator, read this :wink:

http://www.cplusplus.com/doc/tutorial/classes/

That is helpful. Thank you :slight_smile: :slight_smile: :slight_smile:

Hadn't see that site before.
I like this part:

The Features of C++ as a Language
...is a strongly-typed unsafe language.
C++ is a language that expects the programmer to know what he or she is doing, << Seems we see little of this at times! I am no exception 8)
but allows for incredible amounts of control as a result.

Hi,
And thanks to you all for your replies, I think I'm going to have too go over that link a few times, it's just a bit over my head at the minute, remember I've been spoiled by basic, I only just got used to the idea of functions!! I suppose I could go back to the site and take a look at the whole program!! and see how it fits in there. But at least I know a little bit more now...

Regards

Mel.

// classes example
#include <iostream>
using namespace std;

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}
int main () {
  CRectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}

In Arduino, we do not use main() but that is a mainstay of C/C++.
In Arduino, think void setup() or void loop()...

Overly simplified:
So, scope means where a variable can be "seen", that is, where it is active. An object is a complex area of memory that the compiler will keep separate from our void loop() {} area of memory. Think of it as a super function on steroids; however, objects can be rather simple.

Like a function, when we interact with an object, we can do so in a variety of ways depending on complexity. By specifying:
CRectangle rect;
rect.set_values (3,4);

We are simply setting the variables within the object function. From the reference,

we can refer within the body of the program to any of the public members of the object rect as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member.

This combining of data with functions separated in memory (encapsulated) is very powerful. We can conceive of all kinds of library functions that must manipulate private data using complex methods but expose some functionality to the running program.

As you explore Arduino, remember that most users see "simple" but there is a C++ object-oriented powerhouse of an engine running under the bonnet. Hi-octane fuel required XD

Ray

PS:

Here is a nice project implement I on of the frequency counter.
http://www.harryaxten.webs.com/freqcounter.htm

We do use main, it is just hidden in main.cpp.
IDE 1.0.5:

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

I don't know what some of that stuff does.
Compare to -0023 for example

#include <WProgram.h>

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

and then 1.0

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USB.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

We do use main, it is just hidden in main.cpp.

YES, of course it is :astonished:

But I am pretty sure that CactusFace will eventually stumble upon that. Coming from an oppressive PICAXE environment, he is likely giddy with new found capabilities. I wanted to expand on the object oriented nature of C++ without drowning him beneath the details of post-Arduino GUI processing.

Ray

PS: love the 1284! The again for such a well designed board.

Glad the 1284 is working out well for you. I am starting to put together kits for 25 cards, need 3.3V regulators to arrive to finish them up.

Hi,
Thanks for your replies, Thanks again Ray you really try to get the idea over more, what I think really confused me was that I could not see any code that carried these refs:
Perhaps because I now think their in <FreqCounter.h>? and it's the :: that makes the connection, some how, still not sure!

FreqCounter::f_comp= 8; // Set compensation to 12
FreqCounter::start(100); // Start counting with gatetime of 100ms *** This line sets the counting time to 100ms??
while (FreqCounter::f_ready == 0) // wait until counter ready

frq=FreqCounter::f_freq; // read result
Serial.println(frq); // print result
delay(20);

Any help is welcome..
Regards

Mel.

Hi All,
Perhaps someone can tell me where I'm going wrong here. The code is now almost running as it should. Except for the few lines underlined! I have used Serial.println to confirm that tilt=248 and I can see it on the serial monitor.

I think I'm right with if(tilt=etc) {do it} but what when you need an if inside another as in this case can it be nested?? I'm sure it's something simple that I can't quite get to grips with just yet! But I am trying and the more I do the more I like it.

Here's my code.

Regards

Mel.

/*
*******************************
 ;    Filename:     PololuDrive26082013
 ;    Date: 	    10/08/2013		
 ;    File Version: 18082913	
 ;    Written by :   Mel Saunders		
 ;    Function:	     BuggyDrive	
 ;    Last Revision:  26/08/2013
 ;    Target         Uno
 ; ******************************* 
 
 This program demonstrates the use of an L293 dual H bridge.
 This can run 2x DC motors control their speed and direction.
 */

#include <Servo.h>

Servo scanservo;     //Ping Sensor Servo
int Left=180, Centre=90, Right=0;  //Servo angle in Degrees ??Hextronic servo works back to front LEFT=180, RIGHT=0
const int MA1=0,MA2=1,MB1=2,MB2=3;  //Motor pins
const int PWMpin=11;  //Enable pin on L293 for PWM 
const int scanservopin = A0;  // Pin number for scan servo 
int distance,cm,FRQ,TD,A,B,speedy;
long int randNumber,motorSpeed;
int tilt;
boolean go; 

void setup()
{ 
  pinMode(MA1, OUTPUT);      // sets the digital pin as output     
  pinMode(MA2, OUTPUT);      // sets the digital pin as output
  pinMode(MB1, OUTPUT);      // sets the digital pin as output      
  pinMode(MB2, OUTPUT);      // sets the digital pin as output
  pinMode(scanservopin, OUTPUT);
  go = true;
  scanservo.attach(scanservopin);  // Attach the scan servo to pin 11

}

// * * * start of Main program * * * *

void loop()
{
  if (go == true)
  {
    scan();

    do
    {
      distance=scanner(cm);
      delay(200);
      sound(1600,3);
      delay(200);
    }
    while(distance >20);

    startup();
    
  }
  distance=scanner(cm);
  if(distance <=10)
  {
    TD=300;
    Halt(TD);
    BackOff();
    TD=500;
    Halt(TD);
    Radar();
  }

    if(distance >10 and distance <20)
    { 
      analogWrite(PWMpin,95);
      delay(800);
    }
    
    if(distance >20)
    {
      digitalWrite(MA1,HIGH);       //Left motor forward
      digitalWrite(MA2,LOW);
      digitalWrite(MB1,HIGH);      //Right motor forward 
      digitalWrite(MB2,LOW);
      randomSeed(analogRead(3));
      speedy = random(550,2500);      // Speed for turning, reverese, etc.
      motorSpeed=random(70,255);  //random motor speed
 [u][b]     tilt=analogRead(A1);        //Read analogue switches, TILT, etc.
      delay(100);

      
        if(tilt >=245 and tild<=255);
        {
          (BackOff);[/b][/u]
        } 
        
  }  
}// end Main loop


//------------------------------------------------------------------
void startup()
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);

  for (int i= 40; i <= 255; i++) //start slow, increase speed
  { 
    analogWrite(PWMpin,i);
    delay(40);
  }
  go = false;      //Set go to false
}
//-----------------------------------------------------------------
void Halt(int TD)      //Both motors STOP 
{
  digitalWrite(MA1,LOW);           
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);      
  digitalWrite(MB2,LOW);
  delay(TD);
}
//------------------------------------------------------------------
void Rturn()      //Turn right
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,LOW);       //Right motor reverse 
  digitalWrite(MB2,HIGH);
  delay(speedy);
  speedy = random(250,3000);   
} 
//------------------------------------------------------------------
void Lturn()      //Turn left
{
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,HIGH);       //Right motor forward
  digitalWrite(MB2,LOW);
  delay(speedy); 
  speedy = random(250,3000);  
} 
//------------------------------------------------------------------
void Forward()      //forward
{
  digitalWrite(MA1,HIGH);       //Left motor forward
  digitalWrite(MA2,LOW);
  digitalWrite(MB1,HIGH);      //Right motor forward 
  digitalWrite(MB2,LOW);
}

//------------------------------------------------------------------ 

int  Back(int TD)      //Reverse
{
  analogWrite(PWMpin,motorSpeed);
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,LOW);      //Right motor reverse
  digitalWrite(MB2,HIGH);
  delay(speedy);
}
//------------------------------------------------------------------ 
void  BackOff()      //back off!
{
  analogWrite(PWMpin,motorSpeed);
  delay(100);
  digitalWrite(MA1,LOW);       //Left motor reverse
  digitalWrite(MA2,HIGH);
  digitalWrite(MB1,LOW);      //Right motor reverse
  digitalWrite(MB2,HIGH);
  delay(speedy);
}
//------------------------------------------------------------------ 
void sound(int FRQ,int TD)

{
  tone(12,FRQ,TD);
}
//------------------------------------------------------------------
void scan()
{
  for (int i=Right; i <= Left; i++) 
  {
    scanservo.write(i); 
    delay(20);
  }
  scanservo.write(Centre); 
}
//------------------------------------------------------------------
long scanner(long cm)
{
  const int pingPin=7, EchoPin=8;
  long duration;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  pinMode(EchoPin, INPUT);  

  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW); 
  duration = pulseIn(EchoPin, HIGH);
  delay(100);

  // convert the time into a distance
  // inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  delay(100);

  return (cm);
}
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
//------------------------------------------------------------------  
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void Radar()
{
  scanservo.write(180);
  delay(300);
  tone(12,1500,30);
  A=scanner(cm);
  delay(100);
  scanservo.write(0);
  delay(300);  
  tone(12,1500,30);
  B=scanner(cm);
  scanservo.write(90);
  delay(300);  

  if (A>B) 
  {
    Lturn();  //Then turn left
  }
  else  
  {
    Rturn();  //Then turn right
  }
  delay(100);

}
(BackOff);

Whatever you think this is doing, it isn't. If you're trying to call the BackOff function, take a look at the other time you call it and note the difference.

tild
should be easily solvable, once you can explain what you mean by and

( do you mean the operator && ? )

Hi All,
Thanks for your replies, yes I thought it would be a silly mistake, that I cound'ent see for looking. So with that corrected it now calls BackOff, but now does it all the time, so I think I must have the AND part wrong what I intended was that if tilt >=245 AND <=255 call BackOff(). I checked it using the serial monitor and it's around 248!

tilt=analogRead(A1); This line reads from a string of 4x 10k/swiches, a tilt switch being the one in question, the others not connected as yet.

if(tilt >=245 and tilt<=255);
{
BackOff();
}

I just had a thought!! why have I got BackOff in brackets?? Just done another upload and it works!!

Again thanks for your input and help... Remember I'm still a beginner and learning by my mistakes?

Regards

Mel.

        if(tilt >=245 and tilt<=255);

No semicolons after if statements.

Hi Arrch,
Yes and that too!! Perhaps sometimes we just try a little too hard.

Regards

Mel.