Jump to content

Arduino programming

Frode

Hi i got this chip that i bought from ebay:http://www.ebay.com/itm/161863547083?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

iàm loading in these codes:

 

#include <Servo.h>
Servo touch;

//////////////EDIT THIS PART ONLY///////////////
int servoattach = 13; // Pin configuration
int angleup = 80;//Edit the angle where it wont click the screen
int angledown = 50;
int timeup = 30;//Enter the time off the screen in SECONDS
int timedown = 150;//Enter the time on the screen in MILISECONDS (Time must be small)
/////////////////////////////////////////////////

void setup(){
touch.attach(servoattach);

}

void loop(){
int timeupa= timeup*1000;


touch.write(angleup); 
delay(timeupa);// How long it would stay up
touch.write(angledown); 
delay(timedown); 
}

 

 

The problem i have thoe is that i want the int timeup to be longer than 30 seconds. I tried with 60-50-40 and 35 seconds but the arduino just dosen`t do anything when the time has been passed. I don`t know if this is a bug or just a limit. If there is a limit, is there a code to replace it with so i can have a longer time for the servo to stay up?

thanks in advance

Link to comment
Share on other sites

Link to post
Share on other sites

The upper limit for Arduino's "delay()" function is 4,294,967,295 milliseconds (2^32 - 1), or 49.7 days. As such, you should have no problem delaying 45 or 60 seconds. When you says it "does nothing", what do you mean? Does the servo not move after the delay? What you could do, to make sure it is actually executing code after the delay, is modify your code like so:

 

 #include <Servo.h>
Servo touch;

//////////////EDIT THIS PART ONLY///////////////
int servoattach = 13; // Pin configuration
int angleup = 80;//Edit the angle where it wont click the screen
int angledown = 50;
int timeup = 30;//Enter the time off the screen in SECONDS
int timedown = 150;//Enter the time on the screen in MILISECONDS (Time must be small)
/////////////////////////////////////////////////

void setup(){
	touch.attach(servoattach);
}

void loop(){
	int timeupa= timeup*1000;
	touch.write(angleup);
    Serial.println("Beginning timeupa delay");
	delay(timeupa);// How long it would stay up
   	Serial.println("Completed timeupa delay");
	touch.write(angledown); 
	delay(timedown); 
} 

Upload that code using the Arduino IDE, then press Ctrl + Shift + M to open up the serial monitor. While the code is running, you should see the message "Beginning timeupa delay" after the servo has moved up, and the message "Completed timeupa delay" after your designated time delay. If not, something has gone terribly, terribly wrong, and you should return the Arduino, because there is no reason it should be stopping after that line of code. Let me know what happens :)

Link to comment
Share on other sites

Link to post
Share on other sites

On 2016-08-04 at 11:40 PM, Pinguinsan said:

The upper limit for Arduino's "delay()" function is 4,294,967,295 milliseconds (2^32 - 1), or 49.7 days. As such, you should have no problem delaying 45 or 60 seconds. When you says it "does nothing", what do you mean? Does the servo not move after the delay? What you could do, to make sure it is actually executing code after the delay, is modify your code like so:

 


 #include <Servo.h>
Servo touch;

//////////////EDIT THIS PART ONLY///////////////
int servoattach = 13; // Pin configuration
int angleup = 80;//Edit the angle where it wont click the screen
int angledown = 50;
int timeup = 30;//Enter the time off the screen in SECONDS
int timedown = 150;//Enter the time on the screen in MILISECONDS (Time must be small)
/////////////////////////////////////////////////

void setup(){
	touch.attach(servoattach);
}

void loop(){
	int timeupa= timeup*1000;
	touch.write(angleup);
    Serial.println("Beginning timeupa delay");
	delay(timeupa);// How long it would stay up
   	Serial.println("Completed timeupa delay");
	touch.write(angledown); 
	delay(timedown); 
} 

Upload that code using the Arduino IDE, then press Ctrl + Shift + M to open up the serial monitor. While the code is running, you should see the message "Beginning timeupa delay" after the servo has moved up, and the message "Completed timeupa delay" after your designated time delay. If not, something has gone terribly, terribly wrong, and you should return the Arduino, because there is no reason it should be stopping after that line of code. Let me know what happens :)

What i mean is that the servo does not move after 30 seconds. The code is correctly executed under 30 seconds

Link to comment
Share on other sites

Link to post
Share on other sites

On 2016-08-04 at 11:40 PM, Pinguinsan said:

The upper limit for Arduino's "delay()" function is 4,294,967,295 milliseconds (2^32 - 1), or 49.7 days. As such, you should have no problem delaying 45 or 60 seconds. When you says it "does nothing", what do you mean? Does the servo not move after the delay? What you could do, to make sure it is actually executing code after the delay, is modify your code like so:

 


 #include <Servo.h>
Servo touch;

//////////////EDIT THIS PART ONLY///////////////
int servoattach = 13; // Pin configuration
int angleup = 80;//Edit the angle where it wont click the screen
int angledown = 50;
int timeup = 30;//Enter the time off the screen in SECONDS
int timedown = 150;//Enter the time on the screen in MILISECONDS (Time must be small)
/////////////////////////////////////////////////

void setup(){
	touch.attach(servoattach);
}

void loop(){
	int timeupa= timeup*1000;
	touch.write(angleup);
    Serial.println("Beginning timeupa delay");
	delay(timeupa);// How long it would stay up
   	Serial.println("Completed timeupa delay");
	touch.write(angledown); 
	delay(timedown); 
} 

Upload that code using the Arduino IDE, then press Ctrl + Shift + M to open up the serial monitor. While the code is running, you should see the message "Beginning timeupa delay" after the servo has moved up, and the message "Completed timeupa delay" after your designated time delay. If not, something has gone terribly, terribly wrong, and you should return the Arduino, because there is no reason it should be stopping after that line of code. Let me know what happens :)

I have now tried your code and the issue is like last time. Still the timeup limit off 30 seconds. Tried with 40 and 60 seconds. No reaction from the servo.

Link to comment
Share on other sites

Link to post
Share on other sites

While the delay function will indeed take a unsigned long 32 bit value, the temporary variables "timeup" and "timeupa" are of type int which is 16 bit on arduino.

 

because of this, the expression "timeup * 1000" is also of type int. If the result is higher then 32767 it will overflow into a negative value and the conversion to unsigned long will result in a very high value, causing it to look like it's no longer responding.

 

change "int timeup = 30" to "unsigned long timeup = 30" and "int timeupa = timeup * 1000" to "unsigned long timeupa = timeup * 1000".

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Unimportant said:

While the delay function will indeed take a unsigned long 32 bit value, the temporary variables "timeup" and "timeupa" are of type int which is 16 bit on arduino.

 

because of this, the expression "timeup * 1000" is also of type int. If the result is higher then 32767 it will overflow into a negative value and the conversion to unsigned long will result in a very high value, causing it to look like it's no longer responding.

 

change "int timeup = 30" to "unsigned long timeup = 30" and "int timeupa = timeup * 1000" to "unsiigned long timeupa = timeup * 1000".

Thank you for the codes mate. Now i can use the timeup for longer than 30 seconds. Thank you so much.

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/7/2016 at 2:23 PM, Unimportant said:

While the delay function will indeed take a unsigned long 32 bit value, the temporary variables "timeup" and "timeupa" are of type int which is 16 bit on arduino.

 

because of this, the expression "timeup * 1000" is also of type int. If the result is higher then 32767 it will overflow into a negative value and the conversion to unsigned long will result in a very high value, causing it to look like it's no longer responding.

 

change "int timeup = 30" to "unsigned long timeup = 30" and "int timeupa = timeup * 1000" to "unsigned long timeupa = timeup * 1000".

@Unimportant Nice catch!

Link to comment
Share on other sites

Link to post
Share on other sites

wouldn't it be nice if we could all just agree on what an int is?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, vorticalbox said:

wouldn't it be nice if we could all just agree on what an int is?

There is agreement. The standard states that an int must hold at minimum +32767 to -32767 which requires 16 bits. There is however no upper limit on the size. Since arduino (Atmel AVR) is a 8 bit machine they had to make it 16-bit since 8-bit is not allowed.

 

Most compilers implement a int the size of a CPU register. Which is actually very useful. Whenever you use a int you can be pretty sure it is of native size to the target machine and thus efficient. Thus, whenever you need a counter or loop index that you can be sure of will fit in the range +32767 to -32767, make it a int so the target CPU can handle it efficiently.

 

If you need fixed sizes use the header stdint.h which defines fixed size data types like "uint16_t" and so on.

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, Unimportant said:

There is agreement. The standard states that an int must hold at minimum +32767 to -32767 which requires 16 bits. There is however no upper limit on the size. Since arduino (Atmel AVR) is a 8 bit machine they had to make it 16-bit since 8-bit is not allowed.

 

Most compilers implement a int the size of a CPU register. Which is actually very useful. Whenever you use a int you can be pretty sure it is of native size to the target machine and thus efficient. Thus, whenever you need a counter or loop index that you can be sure of will fit in the range +32767 to -32767, make it a int so the target CPU can handle it efficiently.

 

If you need fixed sizes use the header stdint.h which defines fixed size data types like "uint16_t" and so on.

Well said. If I wanted to be REALLY pedantic, I would say something like "16 bits/2 bytes, only for computers which are base 2, and define 1 byte as 8 bits". All that being said, if you want a little more info as to what @Unimportant said, see this Stackoverflow answer.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×