How can I loop through a number of variables in Arduino? - arduino

Basically, I have the following clunky set of if statements in my code put in place to set any erroneous sensor readings to zero.
if (soil_moisture > 150 || soil_moisture <-100){
soil_moisture = 0;
}
if (soil_temperature > 150 || soil_temperature < -100 ){
soil_temperature = 0;
}
if (ambient_temperature > 150 || ambient_temperature < -100 ){
ambient_temperature = 0;
}
if (ambient_humidity > 150 || ambient_humidity <-100){
ambient_humidity = 0;
}
The way it's written seems redundant and inefficient, and I'm wondering, if there's a better way of doing this. Is it possible to create an array of the variables soil_temperature, soil_moisture, etc... and then loop through these variables in a for loop, which has the if statement embedded in it?

It is perfectly fine to do this although a loop would work too. Depending on your idea of "efficient". Is smaller code more efficient? Less writing or less instructions. For the first criteria, a loop would be far more efficient. For the second, this holds true also. Instruction per instruction though, this is a very efficient method.

You could use a helper routine to trim down the code:
boolean outOfBounds(int value, int low, int high)
{
if ( value < low || value > high )
return true;
else
return false;
}
Then in your code ...
if ( outOfBounds(soil_moisture, -100, 150) ) {
soil_moisture = 0;
}
if ( outOfBounds(soil_temperature, -100, 150) ) {
soil_temperature = 0;
}
// etc.
If your limits are always -100 and 150, then you could pass a pointer to the variable, and set it to zero within the routine:
void resetBounds(int *pvar)
{
if ( *pvar < -100 || *pvar > 150 ) {
*pvar = 0;
}
}
In your code:
resetBounds(&soil_moisture);
resetBounds(&soil_temperature);
// etc.
Pretty clean to my eyes.
As for your loop, yes, that would work, but it implies the overhead of copying the values into an array, and then looping over them, which distracts from what you're doing to them. It's almost like the mechaism gets in way of seeing what's happening. Just my opinion.
Have fun!

Related

Palindrome check throws infinite loop (using iterator and linked lists collection)

I´m trying to write a method to determine if a singly linked list of type string is a palindrome.
The idea is to copy the second half to a stack, then use an iterator to pop the elements of the stack and check that they are the same as the elements from 0 to around half of the singly linked list.
But my iterator method is throwing an infinite loop:
public static boolean isPalindrome(LinkedList<String> list, Stack<String> stack ) {
int halfList = (int) Math.ceil(list.size()/2); // we get half the list size, then round up in case it´s odd
// testing: System.out.println("half of size is " + halfList);`
// copy elements of SLL into the stack (push them in) after reaching the midpoint
int count = 0;
boolean isIt = true;
Iterator<String> itr = list.iterator();
Iterator<String> itr2 = list.iterator();
System.out.println("\n i print too! ");
// CHECK!! Node head = list.element();
// LOOP: traverse through SLL and add the second half to the stack (push)
// if even # of elements
if ( list.size() % 1 == 0 ) {
System.out.println("\n me too! ");
while ( itr.hasNext() ) {
String currentString = itr.next(); // this throws an exception in thread empty stack exception
count ++;
if ( count == halfList ) stack.push(list.element());
// THIS IS THE INFINITE LOOP
System.out.println("\n me three! ");
}
}
// else, if odd # of elements
else {
while ( itr.hasNext() ) {
count ++;
if ( count == halfList -1 ) stack.push(list.element());
}
}
// Now we compare the first half of the SLL to the stack (pop off elements)
// even
if ( list.size() % 1 == 0 ) {
while ( itr2.hasNext() ) {
count ++;
if ( count == halfList +1 ) break;
int compared = stack.pop().compareTo(list.element());
if ( compared != 0) isIt = false; // if when comparing the two elements, they aren´t similar, palindrome is false
}
}
// odd
else {
while ( itr2.hasNext() ) {
count ++;
if ( count == halfList ) break;
int compared = stack.pop().compareTo(list.element());
if ( compared != 0) isIt = false;
}
}
return isIt;
}
What am I doing wrong?
There are many issues:
list.size() % 1 == 0 is not checking whether the size is even. The correct check is % 2.
The stack exception cannot occur on the line where you put that comment. It occurs further down the code where you have stack.pop(). The reason for this exception is that you try to pop an element from a stack that has no more elements.
The infinite loop does not occur where you put that comment. It would occur in any of the other loops that you have further in the code: there you never call itr.next() or itr2.next(), and so you'll loop infinitely if you ever get there.
The stack never gets more than 1 value pushed unto it. This is because you have a strict equality condition that is only true once during the iteration. This is not what you want: you want half of the list to end up on the stack. This is also the reason why you get a stack error: the second half of your code expects there to be enough items on the stack.
push(list.element()) is always going to push the first list value to the stack, not the currently iterated one. This should be push(currentString).
count ++; is placed at an unintuitive place in your loops. It makes more sense if that line is moved to become the last statement in the loop.
The if ( count statements are all wrong. If you move count ++ to be the last statement, then this if should read if ( count >= halfList ) for the even case, and if ( count > halfList ) for the odd case. Of course, it would have been easier if halfList would have been adapted, so that you can deal equally with the odd and even case.
The second part of your code has not reset the counter, but continues with count ++. This will make that if ( count == halfList ) is never true, and so this is another reason why the stack.pop() will eventually raise an exception. Either you should reset the counter before you start that second half (with count = 0;) or, better, you should just check whether the stack is empty and then exit the loop.
The second half of your code does not need to make the distinction between odd or even.
Instead of setting isIt to false, it is better to just immediately exit the function with return false, as there is no further benefit to keep on iterating.
The function should not take the stack as an argument: you always want to start with an empty stack, so this should be a local variable, not a parameter.
There is no use in doing Math.ceil on a result that is already an int. Division results in an int when both arguments are int. So to round upwards, add 1 to it before dividing: (list.size()+1) / 2
Avoid code repetition
Most of these problems are evident when you debug your code. It is not so helpful to put print-lines with "I am here". Beter is to print values of your variables, or to step through your code with a good debugger, while inspecting your variables. If you had done that, you would have spotted yourself many of the issues listed above.
Here is a version of your code where the above issues have been resolved:
public static boolean isPalindrome(LinkedList<String> list) {
Stack<String> stack = new Stack<String>();
int halfList = (list.size()+1) / 2; // round upwards
Iterator<String> itr = list.iterator();
while (halfList-- > 0) itr.next(); // skip first half of list
while ( itr.hasNext() ) stack.push(itr.next()); // flush rest unto stack
Iterator<String> itr2 = list.iterator();
while ( itr2.hasNext() && !stack.empty()) { // check that stack is not empty
if (stack.pop().compareTo(itr2.next()) != 0) return false; // no need to continue
}
return true;
}

What is the name of this coding style?

In the past years I have oftener stumpled upon Javascript code that reminded me of the old "Perl" programming days and used boolean terms over if/else clauses. This usually resulted in shorter code with higher complexity per line (and sometimes unnecessary default value creations or empty loops).
I do NOT want to open a discussion on whether this is good or bad practice :-)
But I am curious if there is a name for this compact, "boole-ish" style?
Long / rather "verbose" / lower complexity per line:
function sum(a) {
if (!Array.isArray(a)) {
// No array
return 0;
}
if (a.length === 0) {
// Empty
return 0;
}
var sum = 0;
for (let i = 0; i < a. length; i++) {
sum += a[i];
}
return a;
}
Similar result, with ternary evaluation and ES5 "functional" programming:
// Would we call this "functional" programming? Not sure.
const sum = (arr) => (
Array.isArray(arr) ? arr.reduce((before, value) => before + value, 0) : 0
);
Even shorter, with boolean evaluation:
// How to call this (professionally)?
const sum = (arr) => (
(arr || []).reduce((before, value) => before + value, 0)
);
Note in the last example that the code is the shortest for the human reader, but that it may also create an "unnecessary" array and even evaluate empty loops, i.e. probably more memory consumption and more processing effort.
Just a quick hack for demonstration purposes, no real-world example!

Is there an efficient algorithm for calculating which tiles are within a set walking distance of your character in a 2d grid?

Lets say that on a 2d grid of 20x20, you have your character at position (5,5).
He is able to walk up to 4 tiles in his move. However, there may be obstacles blocking your path such as a wall.
Is there any efficient / easy way for calculating exactly which tiles he would be able to walk to without checking every single possible move ( e.g. move up 0 and right 0 then move up 0 and right 1 e.t.c )?
At the moment I'm calculating the places that you can walk through with this horrific thing:
int playerx = GridPane.getRowIndex(button);
int playery = GridPane.getColumnIndex(button);
int position = playery*8+playerx;
for (int i = 0; i < 5; i++)
{
for (int j = i-4; j < 5-i; j++)
{
try
{
int expectedCollumn = playerx+j;
int actualCollumn = ((position+i+j*8)-((position+i+j*8)%8))/8;
if(expectedCollumn==actualCollumn)
{
Button temp = (Button)gridPane.getChildren()
.get(position+i+j*8);
if (!temp.getText().equals("W") &&
!temp.getText().equals("P"))
{
temp.setText("T");
}
}
actualCollumn = ((position-i+j*8)-((position-i+j*8)%8))/8;
if(expectedCollumn==actualCollumn)
{
Button temp2 = (Button)
gridPane.getChildren().get(position-i+j*8);
if (!temp2.getText().equals("W") &&
!temp2.getText().equals("P"))
{
temp2.setText("T");
}
}
}
}
}
However, its showing as if you are able to walk to the otherside of the wall and I'm not sure how I would go about fixing this.
Many thanks in advance.
For path finding, you should figure out how this works:
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
and then move on to A* or something more efficient.
Thanks to everyone that answered but the solution was simple
incase somehow someone finds this post and is interested it was a simple recursive call
void getReachableTiles(Tile current, Int stamina, List<Tile> visited, List<Tile> reachable) {
if (stamina <= 0) return;
List<Tile> neighbours = new List<>(current + up, current + left, ..)
for (Tile t in neighbours) {
if (!visited.contains(t)) {
visited.append(t);
if (!t.isWall()) {
reachable.append(t);
getReachableTiles(t, stamina - 1, visited, reachable);
}
}
}
}

unnecessary movements in my a-star implementation

I've made an a-star implementation with euclidean heuristics, and it works, but makes unnecessary movements in some situations.
Here is the screenshot:
http://clip2net.com/s/6v2iU4
Path starts on the blue circle and, in theory, cell to the right of it has less F (movement cost + heuristic cost), so a-star takes it first, but it ends up in building not the shortest path.
How can i fix this?
Or a-star is supposed to work this way and i dont need to do anything?
My code: http://pastebin.com/02u33jY6 (h + cpp)
The problem with your algorithm is, your implementation returns the path, when it finds the destination in your openList.
The A* should terminate when destination node is in the closedList, not in the open list.
// check if there is dst node on the closed list
for ( unsigned int i = 0; i < closedList.size(); i++ )
{
if ( closedList.at( i ).getIndex() == dstTileIndex )
{
GSPathFinderNode* backtrackNode = &closedList.at( i );
resultPathNodes->insert( resultPathNodes->begin(), GSCommonMapNode( backtrackNode->getIndex(),
backtrackNode->getPosX(),
backtrackNode->getPosY() ) );
while ( 1 )
{
for ( unsigned int j = 0; j < closedList.size(); j++ )
{
if ( closedList.at( j ).getIndex() == backtrackNode->getParentNodeIndex() )
{
backtrackNode = &closedList.at( j );
resultPathNodes->insert( resultPathNodes->begin(), GSCommonMapNode( backtrackNode->getIndex(),
backtrackNode->getPosX(),
backtrackNode->getPosY() ) );
if ( backtrackNode->getIndex() == srcTileIndex )
return true; // success
}
}
}
}
}

Recursive Sudoku Solver- Segmentation Fault (C++)

I'm attempting to make a sudoku solver for the sake of learning to use recursion. I seem to have gotten most of the code to work well together, but when I run the program, I get a windows error telling me that the program has stopped working. A debug indicates a segmentation fault, and I saw elsewhere that this can be caused by too many recursions. I know this is a brute-force method, but again, I'm more worried about getting it to work than speed. What can I do to fix this to a working level?
struct Playing_grid {
//Value of cell
int number;
//wether the number was a clue or not
bool fixed;
}
grid[9][9];
void recursiveTest(int row, int column, int testing)
{
//first, check to make sure it's not fixed
if(grid[row][column].fixed == false)
{
if((checkRow(testing, row) | checkColumn(testing, column) | checkBox(testing,boxNumber(row,column)) | (testing > 9)) == 0)
{
grid[row][column].number = testing;
moveForward(row,column,testing);
recursiveTest(row, column, testing);
}
else if(testing < 9)
{
testing ++;
recursiveTest(row, column, testing);
}
else if(testing == 9)
{
while(testing == 9)
{
moveBack(row,column,testing);
while(grid[row][column].fixed == true)
{
{
moveBack(row,column,test);
}
}
testing = grid[row][column].number;
recursiveTest(row,column,testing);
}
}
}
else
{
moveForward(row,column,testing);
recursiveTest(row,column,testing);
}
}
void moveForward(int& row, int& column, int& test)
{
if(column < 8)
{
column ++;
}
else if((column == 8) & (row != 8))
{
column = 0;
row ++;
}
else if((column == 8) & (row == 8))
{
finishProgram();
}
test = 1;
}
void moveBack(int& row, int& column, int& test)
{
grid[row][column].number = 0;
if(column > 0)
{
column --;
}
else if((column == 0) & (row > -1))
{
column = 8;
row --;
}
else
{
cout << "This puzzle is unsolveable!" << endl;
}
test++;
}
I tried to include all the relevant pieces. I essentially create a 9x9 matrix, and by this point it is filled with 81 values, where empty slots are written as 0. After confirming the test value is valid in the row, column and box, it fills in that value and moves onto the next space. Whenever it runs to 9 and has no possible values, it returns to the previous value and runs through values for that one.
So as to not overwrite known values, the recursive function checks each time that the value of the grid[row][column].fixed is false.
I'd appreciate any insight as to cleaning this up, condensing it, etc. Thanks in advance!
Edit: To exit the recursive loop, when the function is called to move forward, if it has reached the last cell, it completes (saves + outputs) the solution. The code has been adjusted to reflect this.
I'd normally try to fix your code, but I think in this case it's fundamentally flawed and you need to go back to the drawing board.
As a general rule, the pseudocode for a recursive function like this would be
For each possible (immediate) move
Perform that move
Check for win state, if so store/output it and return true.
Call this function. If it returns true then a win state has been found so return true
Otherwise unperform the move
Having tried every move without finding a win state, return false.

Resources