Notifications
Clear all

Uploading protocol

34 Posts
5 Users
4 Likes
1,542 Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@barrie 

When you're dealing with mixed number types you have to bear in mind their type and limitations as you work.

Generally speaking, integers operating on integers result in integers. floats dealing with anything result in floats.

So, suppose you declare 

int x = 5, y=10;

You'd expect Serial.println(x/y); to report .5 but it would actually give you ZERO. That's because they're both integers the result will be an integer and the closes integer to .5 is zero.

On the other hand

int x=5;

float y=10;

Serial.println(x/y); now reports .5 because the value 5 is converted to a float before the operation, so when the division is done it can produce a floating result (and does so).

You can get really screwed up if you're not careful, so it's best to think about what accuracy you need and maybe do the calculations in floating numbers and then convert to an integer after the calc is complete.

Consider the examples

int x=1, y=3;

float q=50;

now (50*x)/y is 16.66 (as you'd expect) 

but (x/y)*50 is zero ! That's because the brackets force the evaluation of 1/2 (both integers) so that gives zero multiples by 50 which is still zero.

Another tangle you'll find is that each category of numbers has a size limit. For instance, the "byte" type is an 8 bit integer, so it can only hold values with 8 bits (=2 to the power 8 = 256 values). So if you multiply 100 x 100 you get 16 (because you've wrapped it around the clock a few times, just like your odometer).

Anything seems possible when you don't know what you're talking about.


   
DaveE reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@inq Thanks. I thought the same. The answer was in an Int. It should now be an easy fix.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@will Thanks for all that good info. I now know what to do. 

The float was from the output of the sonic module I had to convert it to an integer in order to use it in a Switch Case function.  the conversion was:   25(int derived from float) * 14(int) = 75(int). I will try an integer with the module.  the 25 above was converted from float:  int x  = y where y is a float 

 


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @barrie

@inq Thanks. I thought the same. The answer was in an Int. It should now be an easy fix.

Do you remember about integer overflow/underflow from when you programmed before?  On most computers, they exception when you go over the variable type's limit.  On these microprocessors, they simply wrap to the other side.

I believe on your Arduino, the int is a 16 bit signed integer.  So it goes from -32768 to 32767.  If you were to increment it when it is 32767, it'll actually wrap and its value will be -32768.  Decrementing will hit the bottom and wrap back up to the top.  Point being... If you had something like 

float f = 1020.3;
int i = 42;

// And did...
int j = i * f;

// Internally, the MPU would have:
// 1) upgraded the i to a float = 42.0
// 2) do the multiplication  42.0 * 1020.3 = 42852.6
// 3) In attempting to convert it back to the int so it can place it in j, it would
//    a) truncate it (not round) to 42852
//    b) wrap it since it is higher than 32767 
// So you'd get j = -22684

... is that the "silly answer" type thing you were getting when you were really expecting 42,853?

VBR,

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

yes exactly. one ofthevariables the should have bee 2 digits had about 6  or 7. 


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@inq @will There is something wrong. I have tried all integers, long's and floats. Still get silly answers. It may be a full memoryl. How can I check Eprom ?


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@barrie 

Post your code where it's going wrong so we can see what you're doing.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

its this line in the loop at case 2

 int spd = speedFacter * distance;

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@barrie 

I can't make heads or tails of what you're doing, but you are assigning an int value (ranges from -32,768 to 32,767) to the product of a long value speedFactor (ranges from -2,147,483,648 to 2,147,483,647) times another int (ranges from -32,768 to 32,767).

What value range can spd, speedFactor and distance take ?

I kind of get the feeling that you set spd as an integer so you could use a "switch" statement.  But it seems that a better choice would be to use value ranges if speed is more reasonably represented as a float.

That is, something like

if (spd<10) {

    Serial.println("We appear to have stalled");

} else if (spd<50) {

    Serial.println("Is the parking brake on");

} else if spd<150) {

    Serial.println("So nice to have Tony the Tiger in the tank");

} else if (spd>500) {

    Serial.println("Is it just me or do you hear a siren ?");

}

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 

@barrie 

I just realized that you define ..

 const int speedFacter = 3.3;

which is totally bogus. 3.3 is a floating point number, it has a decimal part and so it can never be 3.3. This value will immediately be truncated to 3.

You MUST use float data types for values which will have non-zero decimal parts !

Anything seems possible when you don't know what you're talking about.


   
Inq reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6971
 

@barrie Please use the REPLY link, and turn on line numbers in your Preferences. While there, turn on other helpful features, like Compiler warnings ALL. 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@zander Didn't see a line number option in Arduino 2.0.0 but they are showing in my sketch. I noticed that I could not select the line numbers with the code when I posted the sketch. I also realised I hit the wrong reply button and almost wrote an apology. here is a belated apology. Sorry. 😇 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6971
 

@barrie Yes, I just checked 2 and it's not there but the sketch has line #'s. I will do a test insert code here to see what happens. While I am nit-picking, it is a lot easier to read code that is uniformlyh formatted. There is a menu item under Tools to auto format the code. It doesn't do much but at least the spacing will be uniform. It's surprising how much that helps. Yep, NO line numbers. I will ask for that to be added.

```cpp
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

```

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@inq It is working but not yet suitable for a video. It is too jerky and I need to get the hang of changing stepper speed on the fly. When it works to spec I will try to remember to post a video. If you have any examples using AccelStepper changing speeds please let me know. 


   
ReplyQuote
Page 2 / 3