Arduino Exiting Loop To Previous Loop - c

So currently I am trying to write code for an arduino car/robot to solve a maze made of black tape on white background. The current sensors I am working with is a wheel encoder(lm393), yellow standard motors, and IR emmiters and detectors in paralell(QTR-8RC).
Currently I am having a problem where I have code to follow the line, and I have code to turn the car around 180 degrees by counting the encoder pulses once it exits the line. The problem is that the car just keeps spinning and that is because it is stuck in the function turnAround();. Is there any way I can go back to the lineFollow(); function once my car is done rotating 180 degrees?
The code used is here:
void turnAround()
{
// NOW THE problem is that the counter is not jumping back to the line follower
while(countRW <= 27 && countLW <= 27) {
printEncoderMesurements();
analogWrite(RWF, 180);
analogWrite(RWB, 0);
analogWrite(LWF, 0);
analogWrite(LWB, 180);
}
resetCounters();
followLine();
}
void followLine() {
if (
((s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 0))||
((s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))||
((s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))||
((s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))||
((s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 0))||
((s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 0))||
((s2 == 0) && (s3 == 1) && (s4 == 0) && (s5 == 0))){
forwards();
}else if(s0 == 1 || s1 == 1) {
slowLeft();
}else if(s7 == 1 || s6 == 1){
slowRight();
}else if((s0 == 0)&&(s1 == 0)&&(s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0) && (s6 == 0) && (s7 == 0)) {
turnAround();
}
}
void loop(){
makeIRReadings();
printEncoderMesurements();
followLine();
delay(50);
}
I have tried every variations of different exit(0) or return 0 but none worked.

Your while loop is checking if countRW and countLW are less than 28, but while its looping in that while loop, you never update countRW or countLW so they will forever be less than 28.
Also, I'm not sure how your sensors are setup, but it seems like you drive off the line before turning around. So then after it turns around, it's still off the line, so it will call turnAround() again. You need a way to "catch" the line after turning around so it could follow it again.

Related

Keep getting warning "Suggest parentheses around '&&' within '||' for C program [duplicate]

This question already has answers here:
Why does Clang warn: `'&&' within '||'`?
(2 answers)
Closed 12 months ago.
printf(" 1| %d | %d | %d | %d | %d\n",
((coffee_strength == 'm' || coffee_strength == 'M') &&
(coffee1_type == 'l' || coffee1_type == 'L') ||
(coffee_strength == 'r' || coffee_strength == 'R') &&
(coffee1_type == 'b' || coffee1_type == 'B')),
((coffee_maker == 'r' || coffee_maker == 'R') &&
(coffee1_grindSize == 'c' || coffee1_grindSize == 'C') ||
(coffee_maker == 'C' || coffee_maker == 'c') &&
(coffee1_grindSize == 'f' || coffee1_grindSize == 'F')),
((coffee_servings >= 1) && (coffee_servings <= 4) &&
(coffee1_weight >= 0) && (coffee1_weight <= 250) ||
((coffee_servings >= 1) && (coffee_servings <= 9) &&
(coffee1_weight == 500)) ||
((coffee_servings >= 10) && (coffee1_weight == 1000))),
(((coffee_cream == 'y' || coffee_cream == 'Y') &&
(coffee1_cream == 'y' || coffee1_cream == 'Y') ||
(coffee_cream == 'n' || coffee_cream == 'N') &&
(coffee1_cream == 'n' || coffee1_cream == 'N'))),
((coffee1_temp >= 60.0) && (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') ||
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
This is my code that I am trying to compile. Everytime I run it I still get the issue related to bracket placement. I have edited my code many times to try and solve this but the issue still persists. Does anyone have any suggestions as to what I should do?
Operator && has higher precedence than operator ||. Therefore, though legal, the compiler is telling you code that does this:
a && b || c && d
May not be doing what you intended. The compiler will treat that as
(a && b) || (c && d)
but for all it knows, you intended any one of a number of other things.
Perhaps this:
a && (b || (c && d))
or... this:
((a && b) || c) && d
Or maybe... this:
a && (b || c) && d
etc.
It makes a difference, and mistakes like this are common, so although your code will compile, it will do so with warnings when appropriately asked to do so (and it's always appropriate to ask). Not because it is wrong, but rather because you didn't make intent clear enough that the warning monkeys were subdued (which in this case, and most cases, is a good thing) To ensure you get what you expected, the compiler is asking you to clarify your expression via parenthesis. The -Wlogical-op-parentheses, included with -Wall, is the likely candidate telling you this if you're using gcc or clang.
By the looks of it, you've taken up the mantle of trying to address this, but you missed several instances. For example:
((coffee_servings >= 1) && (coffee_servings <= 4) &&
(coffee1_weight >= 0) && (coffee1_weight <= 250) || // <== here
((coffee_servings >= 1) && (coffee_servings <= 9) &&
(coffee1_weight == 500)) ||
((coffee_servings >= 10) && (coffee1_weight == 1000)))
There are others below, which I leave for you to find (they're pretty obvious once you start nesting your parens and realize just how many places a && b || c pops up).
(((coffee_cream == 'y' || coffee_cream == 'Y') &&
(coffee1_cream == 'y' || coffee1_cream == 'Y') ||
(coffee_cream == 'n' || coffee_cream == 'N') &&
(coffee1_cream == 'n' || coffee1_cream == 'N'))),
and here:
((coffee1_temp >= 60.0) && (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') || // <== here
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C')))
Sorry to say, but that seems like making out everything by looking at space. There are millions of stars, planets, asteroids, comets, meteors.
But I extremely appreciate your effort on formatting. Not a joke.
The problem seems to be here:
&& (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') ||
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
Let's expand it:
&& (coffee1_temp <= 69.9) && (coffee_maker == 'r' || coffee_maker == 'R') || ((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
I am not sure what you want to acheive in that line, but here is a example that might fix the error in that line:
&& (coffee1_temp <= 69.9) && ( (coffee_maker == 'r' || coffee_maker == 'R') || ((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C')) ) ));
Why does the error happen?
You have a a || c && d/a && b || c && d. The compiler warns you since you did not tell it what to do in that situation.
Example:
a && c || c && d,
What should happen first? c || c or a && c or c || c && d, etc... Extremely confusing...
Edit:
It also seems like you have this on many other places too.
You could just use separate if statements to make this clearer...after all it seems like it takes more time to read this than to write a new set of if statements.
Hopefully more readable:
(
(coffee_strength == 'm' || coffee_strength == 'M')
&& (coffee1_type == 'l' || coffee1_type == 'L')
|| (coffee_strength == 'r' || coffee_strength == 'R')
&& (coffee1_type == 'b' || coffee1_type == 'B')
),
(
(coffee_maker == 'r' || coffee_maker == 'R')
&& (coffee1_grindSize == 'c' || coffee1_grindSize == 'C')
|| (coffee_maker == 'C' || coffee_maker == 'c')
&& (coffee1_grindSize == 'f' || coffee1_grindSize == 'F')
),
(
(coffee_servings >= 1)
&& (coffee_servings <= 4)
&& (coffee1_weight >= 0)
&& (coffee1_weight <= 250)
|| (
(coffee_servings >= 1)
&& (coffee_servings <= 9)
&& (coffee1_weight == 500)
)
|| (
(coffee_servings >= 10)
&& (coffee1_weight == 1000)
)
),
(
(
(coffee_cream == 'y' || coffee_cream == 'Y')
&& (coffee1_cream == 'y' || coffee1_cream == 'Y')
|| (coffee_cream == 'n' || coffee_cream == 'N')
&& (coffee1_cream == 'n' || coffee1_cream == 'N')
)
),
(
(coffee1_temp >= 60.0)
&& (coffee1_temp <= 69.9)
&& (coffee_maker == 'r' || coffee_maker == 'R')
|| (
(coffee1_temp >= 70.0)
&& (coffee_maker == 'c' || coffee_maker == 'C')
)
)
Now, is it really hard to make out what the issues are?
Fix, THAT MIGHT NOT BEHAVE AS YOU INTENDED:
(
(
(coffee_strength == 'm' || coffee_strength == 'M')
&& (coffee1_type == 'l' || coffee1_type == 'L')
)
|| (
(coffee_strength == 'r' || coffee_strength == 'R')
&& (coffee1_type == 'b' || coffee1_type == 'B')
)
)
(
(
(coffee_maker == 'r' || coffee_maker == 'R')
&& (coffee1_grindSize == 'c' || coffee1_grindSize == 'C')
)
|| (
(coffee_maker == 'C' || coffee_maker == 'c')
&& (coffee1_grindSize == 'f' || coffee1_grindSize == 'F')
)
)
(
(
(coffee_servings >= 1)
&& (coffee_servings <= 4)
&& (coffee1_weight >= 0)
&& (coffee1_weight <= 250)
)
|| (
(coffee_servings >= 1)
&& (coffee_servings <= 9)
&& (coffee1_weight == 500)
)
|| (
(coffee_servings >= 10)
&& (coffee1_weight == 1000)
)
)
(
(
(
(coffee_cream == 'y' || coffee_cream == 'Y')
&& (coffee1_cream == 'y' || coffee1_cream == 'Y')
)
|| (
(coffee_cream == 'n' || coffee_cream == 'N')
&& (coffee1_cream == 'n' || coffee1_cream == 'N')
)
)
)
(
(
(coffee1_temp >= 60.0)
&& (coffee1_temp <= 69.9)
&& (coffee_maker == 'r' || coffee_maker == 'R')
)
|| (
(coffee1_temp >= 70.0)
&& (coffee_maker == 'c' || coffee_maker == 'C')
)
)
And as #user3386109 said, you can simplify statements checking for uppercase and lowercase like a == 'A' || a == 'a' to toupper(a) == 'A' using toupper(int c) and tolower(int c) from <ctype.h>

If condition doesn't output properly yet it compiles in C

So it compiles ok, but the output it is not the expected. The issue I think is in the OR operator which does not work properly, or it conflicts with the && operator. But perhaps it's bad written or something. Please let me know. Thanks in advance.
Here's the code:
if((len == 16) && secDigit == (51 | 52 | 53 | 54 | 55)
{
printf("MASTERCARD\n");
exit(0);
}
else if((len == 15) && secDigit == (34 | 37))
{
printf("AMEX\n");
exit(0);
}
else if((len == (16 | 13)) && firstDigit == 4)
{
printf("VISA\n");
exit(0);
}
else
{
printf("INVALID\n");
exit(0);
}
| is the Binary OR operator while what you need is || ,the Logical OR operator.
Syntax:
a|b
(Boolean 1) || (Boolean 2)
Note: Conditional operators evaluate to boolean values
if((len == 16) && (secDigit >= 51 && secDigit<= 55))
{
printf("MASTERCARD\n");
exit(0);
}
else if((len == 15) && (secDigit == 34 || secDigit == 37))
{
printf("AMEX\n");
exit(0);
}
else if((len == 16 || len == 13)) && firstDigit == 4)
{
printf("VISA\n");
exit(0);
}
else
{
printf("INVALID\n");
exit(0);
}

How can I predict the winning position for player X or O in a tic tac toe game and tell the user the position?

so I wrote a function that allows the user to find if any next move or next play for any player X or O who has the turn to play is winning move. The function will print the cell number or ID that if the player places an X or O in it he/she will win the game. If there is more than one cell where the player could place his symbol in and win the game the function should print all the winning cells for that player.
I made a simple implementation of the code but it seems to work but not print right position. Any help would be great. Thanks.
void ListWinningCells(int m, int n, char board[][n])
{
int check = IsValidBoard(m, n, board);
if(check == 1){
// for row:
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[0][2] == 'X' || board[0][2] == 'O')){
printf("Winning cell is 2 for player %c\n", board[0][0]);
}
if((board[1][0] == 'X' || board[1][0] == 'O') && (board[1][2] == 'X' || board[1][2] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
if((board[2][0] == 'X' || board[2][0] == 'O') && (board[2][2] == 'X' || board[2][2] == 'O')){
printf("Winning cell is 8 for player %c\n", board[0][0]);
}
// for column
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[2][0] == 'X' || board[2][0] == 'O')){
printf("Winning cell is 4 for player %c\n", board[0][0]);
}
if((board[0][1] == 'X' || board[0][1] == 'O') && (board[2][1] == 'X' || board[2][1] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
if((board[0][2] == 'X' || board[0][2] == 'O') && (board[2][2] == 'X' || board[2][2] == 'O')){
printf("Winning cell is 6 for player %c\n", board[0][0]);
}
// for diagonal
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[2][2] == 'X' || board[2][2] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
if((board[0][2] == 'X' || board[0][2] == 'O') && (board[2][0] == 'X' || board[2][0] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
}
else{
IsValidBoard(m, n, board);
}
}
Lets take a look at your first if statement:
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[0][2] == 'X' || board[0][2] == 'O'))
If 'X' is on board[0][0] and 'O' is on board[0][2] the statement would be true, but that isn't correct. It should be:
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[0][2] == board[0][0]))
So it would return that the winning cell is 2. You should also consider that some cases are not being taken into account, as cell 1 and cell 3 being the winning cell, I do not know if it is the intention.
You should also consider using for loops to check the positions.

tic tac toe in c, trying to determine who wins

Trying to determine who wins in a game of tic tac toe,I am new to programming. Currently getting user has won after only entering 1 X or O input. Inputs must be entered with 2 int, row and coloumn. Any help is greatly appreciated!
#include <stdio.h>
#include <stdlib.h>
void drawBoard(char board[][3])
{
int rows, columns;
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 3 ; columns++ )
{
if(board[rows][columns]){
printf( "|%c", board[rows][columns] );
}else{
printf("| ");
}
}
printf("|\n");
}
}
int main()
{
char game[3][3]={{0}};
int totalEntry =0,row,column;
char value;
while(totalEntry<=9){
printf("Please choose x or o: ");
scanf("%c",&value);
getchar();
printf("Enter row number: ");
scanf("%d",&row);
getchar();
printf("Enter Column number: ");
scanf("%d",&column);
getchar();
game[row][column] = value;
drawBoard(game);
if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'x')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'x')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'x')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'x')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'x')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'x')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'x')
printf("User x has won!");
if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'o')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'o')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'o')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'o')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'o')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'o')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'o')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'o');
printf("User o has won!");
break;
}
return 0;
}
I would have used something like following, using a loop. Note that since the board is initialized to zero, assigning winner to a board position which still has zero means that a winner isn't found even if the test for all equal passes.
//declare this outside the main while-loop:
char winner=0;
for(i=0;i<3 && !winner;i++) {
if(game[i][0]==game[i][1] && game[i][1]==game[i][2])
winner=game[i][0]; // across
else if(game[0][i]==game[1][i] && game[1][i]==game[2][i])
winner=game[0][i]; // down
}
if( !winner && ( game[0][0]==game[1][1] && game[1][1]==game[2][2] ||
game[0][2]==game[1][1] && game[1][1]==game[2][0] ) )
winner = game[1][1]; // diagonal
if(winner) {
printf( "Winner is %c!\n",winner );
break;
}
IckIckIck.
First, it'll make you life much easier (at least for determining the winner), if you use a one-dimenisional array of 9 elements.
Then, write a function which test one possible win and returns true or false.
bool Test(char board[9], char player, int a, int b, int c)
{
return board[a] == player
&& board[b] == player
&& board[c] == player;
}
Then Test(game, 'x', 0,3,6); -- tests of x wins down left side.
The logic in your conditional is incorrect. Imagine the board looks like this. 0 denotes an empty spot.
0 0 0
0 0 x
0 0 0
The statement if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x') will evaluate to true because game[0][0] is empty game[0][1] is empty and game[0][2] is empty so they are all equal.
Also since game[0][0] is empty, it is not equal to 'x'.
So changing your != to == will help in that regard.

printf() function and displaying %

I am trying to execute a program that prints the numerical value when the && operator returns true and when it returns false. The code is as follows:-
#include <stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("Part I\n");
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%3 == 0) && (b%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%5 == 0) && (b%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%7 == 0) && (b%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
printf("Part II\n");
printf("The AND operator yields: %d\n",(a%2 == 0) && (b%2 == 0));
printf("The AND operator yields: %d\n",(a%3 == 0) && (b%3 == 0));
printf("The AND operator yields: %d\n",(a%5 == 0) && (b%5 == 0));
printf("The AND operator yields: %d\n",(a%7 == 0) && (b%7 == 0));
return 0;
}
The output ( along with my input ) is as follows:-
210
210
Part I
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
Part II
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
Why is the first part behaving in such a manner? This is happening even when I replace && by ||. I am using a Borland C++ Compiler 5.5 . Please Help.
Because if you want to actually display a %, then you must escape it in the printf format string with another %. e.g.
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
^ ^
I've tested this with http://codepad.org/, which I think uses gcc, and the code worked ok. But you might try to add an extra % before a literal % (i.e, %%) so the compiler knows the % that follows is an actual character. Like this:
printf("Part I\n");
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%%3 == 0) && (b%%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%%5 == 0) && (b%%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%%7 == 0) && (b%%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
You are actually using illegal escape sequence character to print % in the first part. Thats why printf is yielding garbage values.
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
^ ^
Here is you are mistaking
It should be like
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
You can also read about all format specifiers used in C.

Resources