Show Posts
Pages: 1 [2] 3 4 ... 356
16  Using Arduino / Programming Questions / Re: Changing modes question with SMlib on: July 27, 2014, 05:30:43 am
It's untested code.

Change the case 10 to case 0
Code:
inline void readSensorsToSerial( )
{
  switch ( readSensorsState )
  {

  case 10 : // REQUEST READ TEMPERATURE

Enter a bunch of random text into serial monitor.
When you enter a non-command, it prints the text. So if you don't enter a printable character, you see nothing.
Serial commands are MSG, TEST and /END. Try those.

If serial is still not working ........... add a line right after this
Code:
inline void hwSerialRead( )
{
  switch ( serialState )
  {

  case 0 :

    if ( Serial.available( ))
    {           
      serialChar = Serial.read( );


Code:
Serial.print( serialChar );

It will make a mess of the screen if hwSerialRead( ) runs and text characters are entered but it should confirm serial reads are made.

17  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 27, 2014, 01:14:09 am
This is where the Arduino Foundations link to Functions and the Sheepdog's link to Functions that return a value comes in. The concepts are better explained in those than there is room for here. Study those and you won't go away missing pieces that hints and clues here will not cover. And you shot right by those links, didn't you?

This sketch has a function, not just a few lines of code but an actual function named myMultiplyFunction that takes 2 integers, multiplies them and returns the product as the result. The function code does not need to know what variable names the 2 integers are from and the code that uses the function does not need to know what variable name the returned value is in because there isn't any. A variable can be thought of as a function that returns the value at a named address in memory. This is what I mean by fundamentals.

Code:
void setup(){
  Serial.begin(9600);
}

void loop() {
  int i = 2;
  int j = 3;
  int k;

  k = myMultiplyFunction(i, j); // k now contains 6
  Serial.println(k);
  delay(500);
}

int myMultiplyFunction(int x, int y){
  int result;
  result = x * y;
  return result;
}

You can run with the above but you might go to the source and make sure that you have all the bases covered.
http://arduino.cc/en/Reference/FunctionDeclaration

The link to Sheepdog's site explanation of functions is even better. And yes, there are caveats to know -- we get "why doesn't" questions about them here on a regular basis.

Relax, don't rush, don't play the "never time to do it right, always time to do it over" game that society often teaches. In the not so long run you will come out ahead.
18  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 27, 2014, 12:21:07 am
I don't think you're ready for the code I posted in the other thread because it uses a few concepts that you're not familiar with, don't have the strength of practice in which is OKAY. Nobody starts knowing that much, it takes time and I see you are how long now just knowing about arrays and loops at all let alone working through the implications.

Programming is like a game with simple rules but deep strategies, and there are many of those. The new player starts with the rules and basic play and picks up the strategies by explanation and doing as they go.
The player who doesn't learn half of the rules may be able to play at all but they don't make good choices, miss a lot of opportunities in playing, don't see enough of how it works to understand what goes on to make strategies.
If you learn poker and only know to get as many of the same number card then you could get dealt a straight or a flush and throw it away trying to get just a pair to bet on. It's not your fault per se but it costs, with code the big cost is time spent going down wrong paths instead of learning about paths.
In games people can explain aspects in ways that are sensible but if any part uses a rule the new player just heard of then for the first time, sensible is not enough for knowing. The 'new' rule needs to be demonstrated and used to see how it fits before sensible becomes sense and understanding.
So when you see code with parts that you understand and other parts that are mysteries, stop and work out the mysteries even if it is just to make sure they work as they should.
Code bugs teach that every letter and symbol counts. If there is any that you are not sure of then you don't know, it is time to go back as far as it takes to know and be sure. You might be able to slap parts together and build a little shed but try the same only bigger or just adding changes and it may come crashing down on you, after a lot of work and maybe expense of course! That is why so many technical people have Murphy's Law posters, to remind them that what you or someone else don't know can bite you harder than you would think.

It's not your fault to get lost unless you plow ahead as if you're sure of what you don't know.

Arduino is fast. Not 3 Gz multi-core fast but magnitudes faster on the scales of buttons and most communications than most people, even well past beginner tend to realize. That is one of the big implications that has a major effect on approach, strategy.

What beginners learn is to use delays to stop processing and let events catch up. The delay is "long enough" when the code works. This is what is taught. It is in the beginners examples and many tutorials. With experience a coder can usually trim the delays down and even use fewer delays and artificial delays.

But delays tend to limit the code to a single path however twisted and complex and deeply nested it gets to do the more and more that is desired. The result is almost always what is called spaghetti code, aka the can of worms that every time it is opened takes a bigger can to put the worms back in -- the can is how much RAM, etc, it takes.

Beginner techniques belong in small code even though they can be used to make very big code indeed.
When the code is used for sequential processes, this then that, the only matter is how much will it take.

When you start dealing with events that need to happen when they are ready, it is time to learn new ways with new ideas and terms like asynchronous and synchronous.
Synchronous is like on time, in order, in step like a military parade.
Asynchronous is when it happens, if it happens, order only as needed, on different paths as needed.
(There is more usually halfway, like a marching band all in ordered step but everyone plays their own music.)

If you want to do many things at once smoothly then you don't have code that holds everything up just because one thing isn't ready. You can do that and it can work when there are only a few things going on but it is klunky and gets klunkier with everything added.
For a button and a led and maybe serial input, go ahead but with 16 buttons and as many led strips the only "works" will involve a user who has to be careful with the buttons and accepting push, hold and wait operation. And that brings it around to what you want, what you can accept, how you get there and when.

You can practice and learn on smaller sketches, step by step, or you can plow onward with what you know.
My choice would be the former and the above is my explanation of why.

19  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 04:56:36 pm
Now immagine I would print everything into the LCD in a single operation. Lets say that would take me 100ms. During those 100ms the CPU would be busy and other important tasks would not be completed.

100ms is 1,600,000 cycles, a long time to Arduino. Even 1ms is enough to do > 1 useful processes.
100ms is also fast enough to be difficult for human eyes to keep up with much of.

Have you learned BlinkWithoutDelay?


20  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 04:43:51 pm
Quote
I want to run the SPI print function once in the loop.
And I want the data to be sent (each time a bit changes) to wait for the loop to be executed again, rather than simply make the change as in the code below. this will give priority to the loop, rather than the LCD output.

Does this make sense? If not please feel free to ask

What you outline is how I handle multiple buttons, leds, etc. But your details don't show it.
I think that you need to spend time learning some fundamentals at the working level.

Maybe a **small short** example printing to serial monitor instead of SPI would help you organize your thoughts.
Let the text be labeled "SPI transfer: " and print hex values, leading zeros or not.
By the time you have that right, you will be able to communicate much more concretely, ask better questions.

I have done those things and it didn't happen right away, I can tell you. I cracked books and wrote code just so I could learn what I was doing. Even if you have a teacher, without practice the lessons are empty. Take your time and you will improve, rush and you will forget as soon as the next part comes along.





21  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 01:39:26 pm
Okay. What you put inside the code tags in the last 2 posts makes no sense to me at all.

You send some data out through SPI then change a bit in the data, wait, and change it again and wait.
Why? I have no clue. Those changes aren't going anywhere, certainly not to SPI.

Code:
digitalWrite (csPin, LOW); // connects to RCLK

SPI.transfer (lowByte (data)); // send out lower 8 bits
SPI.transfer (highByte (data)); // send out upper 8 bits MSB first
digitalWrite (csPin, HIGH); // data output changes on this rising edge


bitSet(data, 3);
delay(500);
bitClear(data, 3);
delay(500);
22  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 01:29:56 pm
...
Answer is to work in smaller units. If I want meters to 3 places then I work in millimeters or smaller depending on what math I will be doing. 32 bit int gives me 9 places. 64 bit int gives me 19 places. Both are much faster than 32 bit float.
Can be effective but not in this case, the point of my reasoning,
 
(180-162)/(184-162) => 0    if all numbers are treated as integer

making them long does not change that

(180000-162000)/(184000-162000)  => 0L

That's fine but for other values you can get 0 using 16 bit and non-zero using 32 bit.
That is a result of precision.
23  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 01:24:00 pm
Quote
very true, but the integer division makes the math less precise as it truncates to 0
straightforward casting to long won't help for the given formula

Answer is to work in smaller units.

newVal = 260000 - (( 260000 - 240000 )  *  ( 180000 - 162000 )  / ( 184000 - 162000 )) = 243636

Code:
void setup( void )
{
  Serial.begin( 115200 );
  Serial.println( );
  
  long range = 260000L - 240000L;
  long scaleMult = 180000L - 162000L;
  long scaleDiv = 184000L - 162000L;
  unsigned long newVal = 260000L - ( range * scaleMult / scaleDiv );

  Serial.println( newVal );
  
  Serial.print( newVal / 1000L );
  Serial.print( "." );
  newVal %= 1000L;
  if ( newVal < 100L )  Serial.print( "0" );
  if ( newVal < 10L )   Serial.print( "0" );
  if ( newVal == 0L )   Serial.print( "0" );
  else Serial.println( newVal );
}

void loop( void )
{
}

I used these techniques for industrial machining (NC/CNC), payroll and billing code long before FPU's became standard default PC hardware. You want to see someone go off, let them find out that their paycheck  is a penny too low or their bill is a penny too high.  smiley-roll
24  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 12:28:46 pm
Is that PIC code?
 
25  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 26, 2014, 12:23:03 pm
Because the serial print is your delay and you have buttons that finish bouncing by the time it's done.

Do you use a delay after serial available to allow the rest of the message to arrive?

26  Using Arduino / Programming Questions / Re: Buttons and Led's - Button switching too quickly on: July 26, 2014, 12:05:29 pm
Arrays, ARRAYS... use ARRAYS

You want to have the arduino remember the last button press state, and if the state does NOT change,( high to low or low to high) then don't do anything. Only when the button changes, do something.

You may also need to debounce the buttons too.

I might have something you can use, let me look.

Code:
const byte button = 2;
boolean state, lastState = LOW;

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");
  pinMode(button, INPUT);
}

void loop()
{
  state = digitalRead(button);
 
  if(state != lastState) // only do something if the button state changes, ie. not held down
  {
    lastState = state; // update lastState
    Serial.print("The buttons state is: ");
    Serial.println(state);
   
    if(state == HIGH) // do something ONLY when the buttons state is HIGH
      digitalWrite(13, !digitalRead(13) );
  }
}


That doesn't address bounce at all.

Bounce is when physical contacts make and break the connection many times in a short period.
Bounce happens over time. If all you do is look for an instant, you can't tell what is happening.

Debounce is when you watch or wait for the bouncing to stop for longer than the bouncing happens.
If my code watches the switch change state back and forth, and it's easy to test-sketch that, then it's bouncing.

Once the state stays the same for some period of time, once it is stable, THEN it's time to set a state that the rest of the code can use as button state.

Unstable button state has some uses but generally the desired logic is based on the ideal people think of rather than the reality we need to code for.

Throw in an array of buttons needing to work at the same time together with an array of leds (strips) and you have something that I gave working examples of how to do 3 days ago, but not all in 1 package.
http://forum.arduino.cc/index.php?topic=256847.msg1816262#msg1816262

I could have posted the 4x4 multiplexed buttons sketch but that's a whole nother step more complicated.
Maybe the thing to do is get it done for 1 button first.

27  Using Arduino / Programming Questions / Re: Set bits in a variable as output ports for serial connections on: July 26, 2014, 11:31:10 am
Here are links to well done ready made lessons/explanations/examples ready already:

Arduino Foundations page:
http://arduino.cc/en/Tutorial/Foundations
Beginner howto on functions:
http://arduino.cc/en/Reference/FunctionDeclaration

A more complete Arduino programming  site:
http://sheepdogguides.com/arduino/FA1main.htm
Functions that return values:
http://sheepdogguides.com/arduino/FA1funcret.htm

More links to Arduino tutorials and example:
http://playground.arduino.cc/Main/TutorialList
http://playground.arduino.cc/Main/GeneralCodeLibrary


28  Using Arduino / Programming Questions / Re: How to create lookup table on: July 26, 2014, 10:15:07 am
I always avoid float math on processors without FPU when possible, and even with, I don't like FP.
Cast the intermediate values to 32 bit, it's more than a magnitude faster.
 
very true, but the integer division makes the math less precise as it truncates to 0
straightforward casting to long won't help for the given formula

Quote
newVal = 260 - ((260-240) * ((180-162)/(184-162)))

as in the division the numerator > denominator


doing the multiplication first improves the result - and there casting to long will prevent overflow.
Quote
newVal = 260 - ((260-240) * (180-162)) /(184-162)

the result is now 244 which is in fact quite good.


Answer is to work in smaller units. If I want meters to 3 places then I work in millimeters or smaller depending on what math I will be doing. 32 bit int gives me 9 places. 64 bit int gives me 19 places. Both are much faster than 32 bit float.


29  Using Arduino / Project Guidance / Re: rpm - program on: July 26, 2014, 10:03:10 am
need a program to find the rpm of a wheel using a proximity sensor...


What kind of proximity sensor and what kind of wheel?

There are induction sensors capable of counting gear teeth as they pass, no magnet on the gear at all.
Such a sensor is not affected by dirt or oil and has no bulb to change.

If there is no dirt, a light and sensor are cheaper to use. Again, gear teeth or holes or slits in a wheel can be used.



 
30  Using Arduino / Programming Questions / Re: Problem with Arduino Micro and putting it to sleep on: July 26, 2014, 09:00:11 am
Try putting a led and resistor on pin 2 and make it light.

Pages: 1 [2] 3 4 ... 356