Hello,
Is it possible to set a variable between specific values?
for example a value only from 0 to 255?
Now I use the unsigned int but I would like to set a certain maximum value.
Thanks in advance.
Hello,
Is it possible to set a variable between specific values?
for example a value only from 0 to 255?
Now I use the unsigned int but I would like to set a certain maximum value.
Thanks in advance.
For 0 - 255 the best would be to use a byte. But then you could assign the value -1, and get the unexpected result 255.
I've made a library for this, it's called Constrain
Constrained example(10,0,255);
example is 10, and can never go below 0, or above 255
Great This is just what I need. Thanks
I get this error:
"3: error: invalid preprocessing directive #inlcude In function 'void loop()':"
I got this error after adding the #inlcude <Constrain.h> library
What does it exactly mean?
http://www.arduino.cc/playground/Code/Constrain#Download
Just PM me for a more thourough explanation. If you feel you need one, that is.
I typed include the wrong way......... Now it is working Thanks
I typed include the wrong way
I constantly write "inlcude" >:(
I think it is because inl are all on the right hand when typing.
I've spent some time debugging that typo.
At least I'm not alone
Another error:
In function 'void loop()':
error: cannot convert 'Constrained' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'
Variables:
Constrained PWMval_Left( 0 , 0 , 255 );
Constrained PWMvalRev_Left( 0 , 0 , 255 );
Constrained PWMval_Right( 0 , 0 , 255 );
Constrained PWMvalRev_Right( 0 , 0 , 255 );
The error is on this line:
analogWrite (PWMpin_Left, PWMvalRev_Left);
Changing Constrained to Constrained also gives the same error.
analogWrite (PWMpin_Left.value, PWMvalRev_Left.value);
It is a bit poorly documented. Will fix that soon.
Another one on the same line:
In function 'void loop()':
error: request for member 'value' in 'PWMpin_Left', which is of non-class type 'int'
Just see me as a beta tester of your library
Try it now: http://www.arduino.cc/playground/uploads/Code/Constrain.zip
analogWrite ((byte)PWMpin_Left, (int)PWMvalRev_Left);
[edit]I think:
analogWrite ((int)PWMpin_Left.value, (byte)PWMvalRev_Left.value);
would solve your problem without downloading the new Constrain.[/edit]
Just see me as a beta tester of your library
Yes, thank you!
Now I get this one:
In function 'void loop()':
error: invalid cast from type 'Constrained' to type 'int'
on this line:
analogWrite ((byte)PWMpin_Left, (int)PWMvalRev_Left);
using these variables:
Constrained PWMvalRev_Left( 0 , 0 , 255 );
int PWMpin_Left = 4;
Is that with version 1.1?
Could you post your code?
Strange behaviour.
Thank you for your patience
[edit]I compiles here. Strange. Maybe you could try to download the Constrain library again :)[/edit]
#include <SNESpad.h>
#include <Constrain.h>
SNESpad nintendo = SNESpad();
int state = 0;
//Motor Control
Constrained<byte> PWMval_Left( 0 , 0 , 255 );
Constrained<byte> PWMvalRev_Left( 0 , 0 , 255 );
int DigPin1_Left = 2;
int DigPin2_Left = 3;
int PWMpin_Left = 4;
char State1_Left = HIGH;
char State2_Left = LOW;
Constrained<byte> PWMval_Right( 0 , 0 , 255 );
Constrained<byte> PWMvalRev_Right( 0 , 0 , 255 );
int DigPin1_Right = 5;
int DigPin2_Right = 6;
int PWMpin_Right = 7;
char State1_Right = HIGH;
char State2_Right = LOW;
void setup() {
Serial.begin(9600);
// Attach motors
pinMode(DigPin1_Left, OUTPUT);
pinMode(DigPin2_Left, OUTPUT);
pinMode(DigPin1_Right, OUTPUT);
pinMode(DigPin2_Right, OUTPUT);
}
void loop() {
// Servo Control SNES PAD
state = nintendo.buttons();
digitalWrite(DigPin1_Left, State1_Left );
digitalWrite(DigPin2_Left, State2_Left);
digitalWrite(DigPin1_Right, State1_Right);
digitalWrite(DigPin2_Right, State2_Right);
if (PWMval_Left = 0)
{
analogWrite ((byte)PWMpin_Left, (int)PWMvalRev_Left);
//analogWrite (PWMpin_Left.value, PWMvalRev_Left.value);
analogWrite (PWMpin_Right.value, PWMvalRev_Right.value);
}
else
{
analogWrite (PWMpin_Left, PWMval_Left);
analogWrite (PWMpin_Right, PWMval_Right);
}
if (state & SNES_UP && (PWMval_Left >= 0 && PWMval_Left < 250) && PWMvalRev_Left == 0)
{
State1_Left = HIGH;
State2_Left = LOW;
State1_Right = HIGH;
State2_Right = LOW;
PWMval_Left = PWMval_Left + 2;
PWMval_Right = PWMval_Right + 2;
Serial.print(" ");
Serial.print(PWMval_Left);
Serial.print(" ");
Serial.print(PWMval_Right);
}}
Here's the code I use. I have tried it with the newest version of the library.
Maybe you can edit my code so I can comply it over here?
I don't mind that I have to be patient. I think its a good and very useful library
Version 1.3: http://www.arduino.cc/playground/uploads/Code/Constrain.zip
//#include <SNESpad.h>
#include <Constrain.h>//SNESpad nintendo = SNESpad();
int state = 0;//Motor Control
Constrained PWMval_Left( 0 , 0 , 255 );
Constrained PWMvalRev_Left( 0 , 0 , 255 );
int DigPin1_Left = 2;
int DigPin2_Left = 3;
int PWMpin_Left = 4;
char State1_Left = HIGH;
char State2_Left = LOW;Constrained PWMval_Right( 0 , 0 , 255 );
Constrained PWMvalRev_Right( 0 , 0 , 255 );
int DigPin1_Right = 5;
int DigPin2_Right = 6;
int PWMpin_Right = 7;
char State1_Right = HIGH;
char State2_Right = LOW;void setup() {
Serial.begin(9600);
// Attach motors
pinMode(DigPin1_Left, OUTPUT);
pinMode(DigPin2_Left, OUTPUT);
pinMode(DigPin1_Right, OUTPUT);
pinMode(DigPin2_Right, OUTPUT);}
void loop() {
// Servo Control SNES PAD
//state = nintendo.buttons();digitalWrite(DigPin1_Left, State1_Left );
digitalWrite(DigPin2_Left, State2_Left);
digitalWrite(DigPin1_Right, State1_Right);
digitalWrite(DigPin2_Right, State2_Right);if (PWMval_Left = 0)
{
analogWrite (PWMpin_Left, (int)PWMvalRev_Left);
analogWrite (PWMpin_Right, (int)PWMvalRev_Right);
}
else
{
analogWrite (PWMpin_Left, (int)PWMval_Left);
analogWrite (PWMpin_Right, (int)PWMval_Right);
}
if (/state & SNES_UP &&/ (PWMval_Left >= (byte)0 && PWMval_Left < (byte)250) && PWMvalRev_Left == (byte)0)
{
State1_Left = HIGH;
State2_Left = LOW;
State1_Right = HIGH;
State2_Right = LOW;PWMval_Left += 2;
PWMval_Right += 2;Serial.print(" ");
Serial.print((int)PWMval_Left);
Serial.print(" ");
Serial.print((int)PWMval_Right);}
}
I've pasted the code into a new file and still receive a error:
In function 'void loop()':
error: invalid cast from type 'Constrained' to type 'int'
I've installed the latest version (1.3) of the library. I've deleted the old folder in Hardware --> library copied the 1.3 version into the folder and restarted Arduino 0015. I assume this is the right way to install the library?
What Arduino version do you use? I use 0015 because I have a Arduino Mega. Can this be the reason?
Best regards.
Hello,
Here (http://arduino.pastebin.com/m460a66f1) is all the code I use.
I use Arduino 0015 (see my previous post).
Best regards.
I see. You've used a pretty old constrain.h
I'm glad this might be it. Started to worry.
http://arduino.pastebin.com/f3918921a
Is what your .h should look like.
Thats strange :o , I've always installed the most recent version I thought.
I'll test is as soon as possible and let you know.
Thanks