Function calling and if statments

Hey guys I wrote some code for project in visual studio and now I wanted to compile it in arduino but I am experincing some wierd behavior for me:

1.cant call function for if statment.
2.cant declere variebles in if statment.

I am not that expert in c more of mechanical engineer, and this things are quiet importand for the algoritim to work, how can I solve this, or should I try to use less functions and do everything in loop?

thanks

It is line 23 that contains the syntax error.

Failing my mind reading abilities post the code. And read the how to use the forum sticke to find out how to do it correctly with code tags.

I thought that it was more of concept question, but if not I apolagy for my mistake

here are some bits from the code with the erors above:

///declering not in scope eror
    if(NewCourse)
        {
                float longtitudeS;//WILL GET FROM GPS
		float latitudeS;//
      ......

} 


///calling function mistake

if (W <= 45 || W>= 315)
		{
			Galsim(a,b,c...);
                 }

You need to look at the syntax this is basically C
The first example makes no sense at all. An if statement has to have a condition to test if that test is passed then the statement or the block filling it is done. A block is defined. By curly braces.

if( var >  3) inr newVar = 0

But this is very bad practice.

Calling a function is right but there are no dots in it. You must have the same number of parameters as the function the function must also be defined somewhere.

projectsira:
Hey guys I wrote some code for project in visual studio and now I wanted to compile it in arduino

I know this is a silly question, but I will ask it anyway.

Is the visual studio code written for a program that runs on your PC?

If so it won't work on an Arduino.

Post the entire code if you want assistance.

...R

Grumpy_Mike:
You need to look at the syntax this is basically C
The first example makes no sense at all.

I waned to do it for remembering the starting course and not using this varibles each loop but only in start thats why I thought about using a boolean that will be tru only in start and do all the stufff i need in first if, but if its not possible then the conecpt cant work.

Robin2:

projectsira:
Hey guys I wrote some code for project in visual studio and now I wanted to compile it in arduino

I know this is a silly question, but I will ask it anyway.

Is the visual studio code written for a program that runs on your PC?

If so it won't work on an Arduino.

Post the entire code if you want assistance.

...R

no its for arduino, its abit long code,my first try in project and the dealine is near, but i thought about understanding the main mistakes and dealing with the algotirim by myself.

the calling still doesnt work and its impotant coz I dont always want to call specific function, from what I understand now its better to write all in the loop?

projectsira:
I waned to do it for remembering the starting course and not using this varibles each loop but only in start thats why I thought about using a boolean that will be tru only in start and do all the stufff i need in first if, but if its not possible then the conecpt cant work.

What on earth does all that mean.
I recognise words but no sense emerges.

Yes you can use boolean variables in an if statement - if that is what the question was.

What language are you writing in?

What a rude answer.

What a rude answer.

Not really. Grumpy Mike is simply trying to get you to clearly state what your issues are because those of us who speak English as a first language are having trouble understanding what you are saying.

Oh, and by the way, when you are lucky enough to get help from a Forum member with over 32000 posts and more than 550 karma points, you would be better served not to piss him off.

@projectsira

I repeat ... one time only ...

Post your full code if you want help.

...R

PS. neither can I understand the post that @Grumpy_MIke referred to.

Hey,

With all the respect,there is manner of answering,thats my opinion.

but anyway on the topic, I wasnt sure I can post here code of full project or something abit longer then few statment, but I will just change the notes to engish in my code and post it here.

thanks.

I wasnt sure I can post here code of full project or something abit longer then few statment

Read Read this before posting a programming question ... - Programming Questions - Arduino Forum for advice on posting code.

When I said:-

What language are you writing in?

I was referring to the computer language that I assume works on your system and that you are trying to convert.

projectsira:
but anyway on the topic, I wasnt sure I can post here code of full project or something abit longer then few statment, but I will just change the notes to engish in my code and post it here.

No need to change the comments into English we are not trying to debug the code here we just need to know what errors you are making so we can tell you how to correct the syntax.

With all the respect,there is manner of answering, thats my opinion.

How would you like to be told that what you have written can not convey any meaning?

ok sorry my friend didn’t understand your meaning,

here is my code:

#include <Servo.h>
#include <SoftwareSerial.h>
#include <Wire.h>

//????? ?????? ?????????
Servo Sailservo;//????? ???? ????
Servo Rudderservo;//????? ???? ???
Servo ESC;//????? ???? ???????
bool NewCourse = true;//???? ????? ????? ???? ??????? ?????? ???,????? ????


///????
int HMC6352SlaveAddress = 0x42;
int HMC6352ReadAddress = 0x41; //"A" in hex, A command is: 
int headingValue;


///GPS
SoftwareSerial gpsSerial(10, 11); // RX, TX (TX not used)
const int sentenceSize = 80;
char sentence[sentenceSize];

///encoder
int val;
int encoder0PinA = 3;
int encoder0PinB = 2;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;



void setup() {



	///????
	///The Wire library uses 7 bit addresses throughout.
	//If you have a datasheet or sample code that uses 8 bit address, 
	//you'll want to drop the low bit (i.e. shift the value one bit to the right), 
	//yielding an address between 0 and 127."
	HMC6352SlaveAddress = HMC6352SlaveAddress >> 1; // I know 0x42 is less than 127, but this is still required
    Serial.begin(9600);
	Wire.begin();

    ////GPS
	gpsSerial.begin(9600);

	//ENCODER
	pinMode(encoder0PinA, INPUT);
	pinMode(encoder0PinB, INPUT);

	////?????
    Sailservo.attach(9);//????? ???? ???? ???? 9
	Rudderservo.attach(10);//????? ???? ??? ???? 10
	ESC.attach(8);//????? ????? ????????
}


void loop() {


	///GPS
	static int i = 0;
	if (gpsSerial.available())
	{
		char ch = gpsSerial.read();
		if (ch != '\n' && i < sentenceSize)
		{
			sentence[i] = ch;
			i++;
		}
		else
		{
			sentence[i] = '\0';
			i = 0;
			displayGPS();
		}
	}








////????

	//"Get Data. Compensate and Calculate New Heading"
	Wire.beginTransmission(HMC6352SlaveAddress);
	Wire.write(HMC6352ReadAddress);              // The "Get Data" command
	Wire.endTransmission();

	//time delays required by HMC6352 upon receipt of the command
	//Get Data. Compensate and Calculate New Heading : 6ms
	delay(6);

	Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB

	//"The heading output data will be the value in tenths of degrees
	//from zero to 3599 and provided in binary format over the two bytes."
	byte MSB = Wire.read();
	byte LSB = Wire.read();

	float headingSum = (MSB << 8) + LSB; //(MSB / LSB sum)
	float headingInt = headingSum / 10;

	Serial.print(headingInt);
	Serial.println(" degrees");

	delay(100);



	////????? ???
	int sensorValue  = analogRead(1);//????? ?????? ?????
	Serial.println(sensorValue, DEC);

	delay(500);               // wait for a second





	///encoder
	delay(1);
	n = digitalReadFast(encoder0PinA);
	if ((encoder0PinALast == LOW) && (n == HIGH)) {
		if (digitalReadFast(encoder0PinB) == LOW) {
			encoder0Pos--;
		}
		else {
			encoder0Pos++;
		}
		if (encoder0Pos >= 360)  { encoder0Pos = encoder0Pos - 360; }
		if (encoder0Pos <= 0)  { encoder0Pos = encoder0Pos + 360; }

		Serial.print(encoder0Pos);
		Serial.println("  degrees");
	}
	encoder0PinALast = n;




	////???? ?????? ????????
	int ServoAng = Sailservo.read();//???? ???? ????? ???? ??
	int BoatSpeed;//????? ????? ????? ???'????
	int RWindSpeed = sensorValue;//???? ?????? ???? ?????? ????? ???
	int WindSpeed = sqrt(BoatSpeed ^ 2 + RwindSpeed ^ 2);
	int WindAng = encoder0Pos;//???? ???? ??? ????????
	float CourseNow = headingInt;//???? ???? ???? ????????? ???? ??
    double longtitudeN;//  ??'????? ?????? ???? ?????? ???? ?????*******************************
	double latitudeN;
	
	////????? ?????,???? ???? ???? ??? ????? ?? ?????? ?? ?????? ??? ?????? ????
	if (NewCourse)
	{
		float longtitudeE;//?????? ???? ?? ????? ????? ??????? ?????? ???,?????? ????? ????? ???
		float latitudeE;
		float longtitudeS;//?????? ???? ?? ????? ????? ???'??
		float latitudeS;//
		float Rs = sqrt(square(longtitudeE - longtitudeS) + (square(latitudeE - latitudeS));
		float Alpha = StartFunction(longtitudeE, latitudeE, longtitudeS, latitudeS);//////////////????? ????????? ????? ????? ??????? ????
	}



	////????? ??????? ?????? ???????? ??????? ????? ????
	float AlphaN = StartFunction(longtitudeE, latitudeE, longtitudeN, latitudeN);
	Fixing(Alpha,AlphaN);//????? ????????? ???????? ??????


	////????? ???? ?????:?????,?????,??? ????
    if (WindSpeed < 1.5)MotorFunction();//????? ????? ????? ??????? ???? ????? ???
	else
	{
		TurnOffMotor();//????? ???? ???? ????? ?? ????
		if (WindAng <= 45 || WindAng >= 315)
		{
			Galsim(Rs,longtitudeE;latitudeE,longtitudeS;latitudeS,WindAng,ServoAng,WindSpeed, Alpha, AlphaN);//????? ????? ???? ????-????? ?? 45 ?????
			continue;
		}
		ServoAng = SailFunction(WindAng, ServoAng, WindSpeed); //????? ????????? ????? ?????? ?????? ????? ????? ?????? ???? ?????
	}
	
	
	///????? ??? ?? ??? ??????
	double R = sqrt(square(longtitudeE - longtitudeN) + (square(latitudeE - latitudeN));//???? ?????? ????
	finsih(R);//????? ????????? ?????



	////????? ??????
    delay(1000);//????? ????? ??? ?? ????? ???? ?? ???? ????? ???

}


void displayGPS()
{
	char field[20];
	getField(field, 0);
	if (strcmp(field, "$GPRMC") == 0)
	{
		Serial.print("Lat: ");
		getField(field, 3);  // number
		Serial.print(field);
		getField(field, 4); // N/S
		Serial.print(field);

		Serial.print(" Long: ");
		getField(field, 5);  // number
		Serial.print(field);
		getField(field, 6);  // E/W
		Serial.println(field);
	}
}

void getField(char* buffer, int index)
{
	int sentencePos = 0;
	int fieldPos = 0;
	int commaCount = 0;
	while (sentencePos < sentenceSize)
	{
		if (sentence[sentencePos] == ',')
		{
			commaCount++;
			sentencePos++;
		}
		if (commaCount == index)
		{
			buffer[fieldPos] = sentence[sentencePos];
			fieldPos++;
		}
		sentencePos++;
	}
	buffer[fieldPos] = '\0';
}


int StartFunction(double longtitudeE, double latitudeE, double longtetudeS, double latitudeS)//??????? ?????? ???? ???? ???????
{
	    int Alpha = atan((longtitudeE-longtetudeS) / (latitudeE-latitudeS));//???? ???? ?? ??? ????? ??????? ?????
		NewCourse = false;//??? ?? ???? ???
		return Alpha;
}


void Fixing(float Alpha, float AlphaN,float CourseNow)//???????? ?????? ?????? ????? ?????? ???? ????
{
	int RudderAng = RudderServo.read();//????? ????? ????
	while (AlphaN <= (Alpha + 1) && AlphaN >= (Alpha - 1))//????? ?????? ?? ????? ?? ????? ?????? ????? ?? ???? ??????? ?? ????
	{
		if (CoursenNow > Alpha)RudderServo.write(RudderAng--5);//,????? ??????? ??? ??? ????? ?? ?????? ????? ????
		else RudderServo.write(RudderAng++5)
	}
	delay(1000);//???? ??? ???? ??????
}

......

Two big erors I get: declearing in if statment, and calling function in if statment.

	delay(500);               // wait for a second





	///encoder
	delay(1);
	n = digitalReadFast(encoder0PinA);

These three lines of code do NOT go in the same sketch. The whole purpose of digitalReadFast() is to speed up reading digital pins. The few microseconds saved are completely dwarfed by the stupid delays.

Two big erors I get: declearing in if statment, and calling function in if statment.

But, you don't intend to share them. Well, good luck with that approach.

Two big erors I get: declearing in if statment, and calling function in if statment.

I do not see those errors in the list. One error that sticks out is the misplaced continue. Your program flow is currently not what you expect, and some of the errors are probably due to missing ‘}’, ‘;’, or ‘)’ characters.
You must also have code somewhere else that is not included in your sketch code.

sketch_may29b.ino: In function ‘void loop()’:
sketch_may29b.ino:130: error: ‘digitalReadFast’ was not declared in this scope
sketch_may29b.ino:153: error: ‘RwindSpeed’ was not declared in this scope
sketch_may29b.ino:166: error: expected )' before ';' token sketch_may29b.ino:173: error: 'longtitudeE' was not declared in this scope sketch_may29b.ino:173: error: 'latitudeE' was not declared in this scope sketch_may29b.ino:174: error: 'Alpha' was not declared in this scope sketch_may29b.ino:178: error: 'MotorFunction' was not declared in this scope sketch_may29b.ino:181: error: 'TurnOffMotor' was not declared in this scope sketch_may29b.ino:184: error: 'Rs' was not declared in this scope sketch_may29b.ino:184: error: expected )’ before ‘;’ token
sketch_may29b.ino:184: error: ‘longtitudeS’ was not declared in this scope
sketch_may29b.ino:184: error: ‘latitudeS’ was not declared in this scope
sketch_may29b.ino:184: error: expected ;' before ')' token sketch_may29b.ino:<mark>185</mark>: error: <mark>continue statement not within a loop</mark> sketch_may29b.ino:187: error: 'SailFunction' was not declared in this scope sketch_may29b.ino:193: error: 'finsih' was not declared in this scope sketch_may29b.ino: In function 'void Fixing(float, float, float)': sketch_may29b.ino:256: error: 'RudderServo' was not declared in this scope sketch_may29b.ino:259: error: 'CoursenNow' was not declared in this scope sketch_may29b.ino:259: error: expected )’ before numeric constant
sketch_may29b.ino:260: error: expected )' before numeric constant sketch_may29b.ino:261: error: expected ;’ before ‘}’ token

Yes of course all the ,. I will fix, I did but saved it in other file nevermind,

what you mean I don't share ? the things I specified are in this part of the code.
here are the erors:
saar_5_tikun.ino: In function 'void loop()':
saar_5_tikun:174: error: 'longtitudeE' was not declared in this scope
saar_5_tikun:174: error: 'latitudeE' was not declared in this scope
saar_5_tikun:175: error: 'Alpha' was not declared in this scope
saar_5_tikun:185: error: 'Rs' was not declared in this scope
saar_5_tikun:185: error: expected )' before ';' token saar_5_tikun:185: error: 'longtitudeS' was not declared in this scope saar_5_tikun:185: error: 'latitudeS' was not declared in this scope saar_5_tikun:185: error: expected ;' before ')' token
saar_5_tikun:186: error: continue statement not within a loop
saar_5_tikun:188: error: void value not ignored as it ought to be
saar_5_tikun:194: error: 'finsih' was not declared in this scope
saar_5_tikun.ino: In function 'void Fixing(float, float, float)':
saar_5_tikun:257: error: 'RudderServo' was not declared in this scope
saar_5_tikun:260: error: 'CoursenNow' was not declared in this scope
saar_5_tikun:260: error: expected )' before numeric constant saar_5_tikun:261: error: expected )' before numeric constant
saar_5_tikun:262: error: expected ;' before '}' token saar_5_tikun.ino: At global scope: saar_5_tikun:267: error: expected )' before ';' token
saar_5_tikun:267: error: expected unqualified-id before 'double'

ignoring the typing mistakes, its half of the program coz it dont give me to put it full.

second part if its important.:

void Galsim(double Rs, double longtitudeE; double latitudeE, double longtitudeS, double latitudeS, int WindAng, int ServoAng, int WindSpeed, int Alpha, int AlphaN)
{
	double HalfR = Rs / 2;//????? ????? ?????? ????? ??????
	double A = 0;//????? ??????? ?? ?????? ?????? ???????, ???? ?????? ???? 
	double R = 11;
	double longtitudeN = 0;
	double latitudeN = 0;
		RuddServo.write(45);//???? ???
		While((A*cos(45)) < HalfR)//??? ?????? ?? ????? ??? ?????? ?? 45 ?????,?? ??? ???? ?????? ???? ??? ?? ?????? 
		{
			longtitudeN;//  ??'????? ?????? ???? ?????? ???? ?????
			latitudeN;
			A = sqrt(square(longtitudeN - longtitudeS) + (square(latitudeN - latitudeS));//???? ??? ?? ???? ?????? ????
			SailFunction(int WindAng, int ServoAng, int WindSpeed); 
			if (WindAng >= 45 && WindAng <= 315) return;//  ????? ????????? ????? ?? ??? ???????
        }
			
		RuddServo.write(-45);//???? ??? ?????? ????
		While(R>=10)
		{
			longtitudeN;//  ??'????? ?????? ???? ?????? ???? ?????
		    latitudeN;			
			
			Fixing(Alpha, int AlphaN)
            R = sqrt((longtitudeE - longtitudeN) ^ 2 + ((latitudeE - latitudeN) ^ 2);			
			SailFunction(int WindAng, int ServoAng, int WindSpeed)
			if (WindAng >= 45 && WindAng <= 315) return;
		   

         }
}	


void SailFunction(int WindAng, int ServoAng, int WindSpeed)//??????? ????? ?? ???? ???? ????? ???? ??? ???? ????? ????? ?????? ?? ?????
{
	if (WindSpeed > 8)
	{
		Sailservo.write(0);//???? ????? ?????? ????? ?? ?????? ??? ????? ???
		continue;
	}
	//???? ???? ???????? ?????? ????? ??????????,?????? ?? 70 ????? ?? ??? ?? ????? ??????,?????? ???,???? 90 ??? ???? ?-"0" ????? ????? ??????? ?0 ??? ?????? ??????
	if ((WindAng>125&& WindAng<=180)
		Sailservo.write((WindAng-90)-15);
	if( WindAng<=70)
		Sailservo.write((WindAng+90) - 15);
	if ((WindAng>180 && WindAng<235)  
		Sailservo.write((WindAng-90) + 15);
	if(WindAng>290 )
		Sailservo.write((WindAng-270) + 15);
	if (WindAng>70 && WindAng<125)
		Sailservo.write(160);
	if (WindAng>235 && WindAng<290)
		Sailservo.write(20);
}


void MotorFunction()
{
	Sailservo.write(0);
	ESC.write(135); //???? ???? ??????? ???? ????? ???????? ????????? ( ???? ??? 90?-180) ??? ????? ????  ???? ???? ???????? ??? ????? ???? ???? ??? ??????? ????? ????????
}


void finish(double R)
{
	if (R < 3)NewCourse = true;;//?? ????? ??? ????? ????? ???? ?????? ?? ????? ????? ???? ,???? ?? ???? ?????? ???
}


void TurnOffMotor()//??????? ?????? ?????? ?? ?????
{
	int EscAng = ESC.read();//????? ??????

	while (ESC>100)//90 ????? ?????? 0
	{
		EscAng = EscAng - 10  //????? ?? ?????? ?????,???? ????? ???? ??? ?? ???? ??? ??? ?????
		ESC.write(EscAng);
		delay(1000);//????? ?????? ????
	}

	ESC.write(90);//????? ????? ????? ??????

}

about it digitalwritefast command i left it here but it shouldnt be digitalwritefast becouse it dont compile me, forgot to change it back, but its in other thread in the forum:)

thanks

ignoring the typing mistakes, its half of the program coz it dont give me to put it full.

You want us to ignore typing mistakes? Did you select that option in the compiler? I’m sure it does NOT ignore typing mistakes.

I, for one, am not going to try to paste your code back together. If you can’t fit it all in one post, you’ve waited far too long to try compiling. There is an option, under Additional Options… to post your code as ONE file. Take advantage of that feature.