| Arduino Forum ::  Members :: GoForSmoke
 Show Posts Pages: 1 2 [3] 4 5 ... 358
 31 Using Arduino / Programming Questions / Re: multiple if or for loop with if inside on: July 28, 2014, 12:47:31 am Quote from: capicoso on July 28, 2014, 12:02:30 amthanks. I'm fine with the performance, but I was wondering because I wanted to optimize a part of my sketch, just because it's a bunch of ugly and LARGE if. It's a slighty different approach than that one. Here it goes, tell me what you thinkCode:if(array[15] == HIGH){counterTest = 14;}if(array[15] == HIGH && array[14] == HIGH){counterTest = 13;}if(array[15] == HIGH && array[14] == HIGH && array[13] == HIGH){counterTest = 12;}... and so on until I have an inmensely big conditional.I needed to know which buttons are high from right to left(in the array) only when they are consequent. I didn't like that part of the code(but it was the first fix it came to mind) so tonight I made it "better" this way:That's probably an unneccesary mess. Are the values in array[] other than 0 or 1?Suppose that you wanted to make array bigger or smaller? QuoteCode:for(int i = 15; i >= 0; i--){ //scans the 16 buttons from higher to lower     if(array[i] == LOW){  // from higher to lower, if it finds a button low, make the counterTest stop summing up       stop = HIGH;          }     if(array[i] == HIGH){ //if it finds any that's high, increments the counter       if(stop == LOW){       counterTest++;       }     }   }stop = LOW; //reset the variablescounterTest = 0;I like way better the latter code, and it may help me optimize other parts of the code with that counterTest value. But, in that case i'm running 2 if inside a for of 16 loops, so it's like 32 ifs..Have you learned about else or break?Code:     if(array[i] == LOW)  // from higher to lower, if it finds a button low, make the counterTest stop summing up     {       stop = HIGH;  // if you don't need this after the loop then break takes care        break; // will exit the loop     }     else //if it's not LOW then it has to be HIGH, if LOW else HIGH     {        counterTest++;     }If you read your digital pins that can only be LOW/HIGH (as 0/1) into the bits of an unsigned int then it becomes much much easier/faster to tell all kinds of things about the states of the buttons and relationships between them.Bit math understanding will let you write powerful and fast code. Bit logic does all the bits in one command, few to one cycle depending on 8 or 16 bits and what's already in the CPU registers.  It can take days to weeks to get comfortable with but so can driving a car.http://playground.arduino.cc/Code/BitMath
 32 Using Arduino / Audio / Re: Arduino library for WTV020-SD-16P audio module on: July 27, 2014, 03:08:40 pm I know what you mean about the confusion.A little extra time in initialization hardly impacts speed anyway.
 33 Using Arduino / Audio / Re: Arduino library for WTV020-SD-16P audio module on: July 27, 2014, 09:02:29 am It might be better to watch the busy pin than to delay and assume readiness.Best case with the delay is you wait every time longer than needed.
 34 Community / Bar Sport / Re: Something for the new and not new programmers. on: July 27, 2014, 08:57:43 am Me too. It's cheap version control and it keeps a history. I am starting to write-protect anything I am not working on.
 35 Using Arduino / Programming Questions / Re: Changing modes question with SMlib on: July 27, 2014, 08:45:00 am Sensor values are in arrays with count.Code:const byte thermometers = 3;DeviceAddress Thermometer[ thermometers ];int temperature[ thermometers ];Add a sensor, change the value of thermometers and it should work.Perhaps serial prints should all start with the value of millis() to make a proper log.I think I should add a RAM check command to the serial section, just to keep an eye on that too.Maybe the longer time is because the 4th sensor is not set up in setup(). Something else to check.
 36 Using Arduino / Programming Questions / Re: Changing modes question with SMlib on: July 27, 2014, 08:00:31 am I updated the change to my copy, also moved Address back 1 space.The sensor reads are happening 30 seconds apart now? If so and no other issues there is GSM and LCD parts and then buttons and more sensors?I am not ready to dive into those right now. Maybe the sketch needs a long run test anyway just to make sure there is no "down the road" bug(s).Do you see how I got rid of the delays, using state machines and time checks?The sensor requests take almost 1/10th second. If all they do is hold up noticing a button or serial then I don't think it's a big deal, much as I don't like it on principle. Maybe the library needs a state machine and time or other check, like answer on the 1-wire bus, too.