interact with some digital ports (on the Arduino Uno) somehow? If so, how? I had an IR receiver hooked up to digital port 2 and my logic was not working in my program that used the millis() function. Switched the IR receiver port from 2 to 5 now my program is working fine.
What program was that?
No, millis does not interact with any pins.
Maybe pin 2 is damaged on your Uno.
Hello jhammons
We need some additional information.
Post your sketch, well formated, with well-tempered comments and in so called code tags "< code >" and a detailed circuit diagram to see how we can help.
Have a nice day and enjoy coding in C++.
[Mod edit: Reply to AI generated answer removed]
the following sets pin 2 to turn on an LED it remains On or Off while a timer is used to toggle pin 4
const byte Pin2 = 2;
const byte Pin3 = 3;
const byte Pin4 = 4;
unsigned long msecLst;
void loop() {
unsigned long msec = millis ();
if (msec - msecLst > 500) {
msecLst = msec;
digitalWrite (Pin4, ! digitalRead (Pin4));
}
}
void setup() {
Serial.begin (9600);
pinMode (Pin2, OUTPUT);
pinMode (Pin3, OUTPUT);
pinMode (Pin4, OUTPUT);
#if 0
digitalWrite (Pin2, HIGH);
#else
digitalWrite (Pin2, LOW);
#endif
delay (2000);
}
Code please?
The millis() function on the Uno is this:
millis()
does no more than read a variable in an interrupt-safe way.
The back-end work that maintains 'timer0_millis
' is in the TIMER0_OVF_vect
Interrupt Service Routine which happens in all sketches, whether or not millis() is actually called.
Does pin 2 still work?
How does your unseen IR code use a pin? Does it change the timer configurations on the pins it is attached to? If so, those changes could impact the timing of TIMER0_OVF_vect ISR calls and therefore millis()
I have removed the AI generated nonsense and associated replies.
Sorry, but the answer you marked as the solution is nonsense, I have deleted it. I am sure the other forum members will be able to help solve your problem.
Thanks,
For those that asked to see the code here it is. Hopefully it is formatted correctly. This is the first time I've pasted code to this site. In my op, I said I changed the IrReceiver from pin 2 to pin 5 and it worked. It is still currently listed as pin 2 in this code.
#include <IRremote.hpp>
#include <Servo.h>
Servo grabber;
Servo servoLeft;
Servo servoRight;
bool blueTeam = false;
bool autonomousHasRun = false;
unsigned long autoEndTime = 0;
unsigned long driverEndTime = 0;
void setup() {
Serial.begin(9600);
IrReceiver.begin(2, DISABLE_LED_FEEDBACK); // Start the receiver on pin 2
grabber.attach(10); // attach the grabber servo to pin 10
servoLeft.attach(13); // attach the left motor servo to pin 13
servoRight.attach(12); // attach the right motor servo to pin 12
resetGrabber(); // set the grabber to 90 degrees
while(!IrReceiver.decode()) { // while no buttons are being pressed...
// do nothing!!
}
autoEndTime = millis() + 20000;
driverEndTime = autoEndTime + 120000;
}
void loop() {
// if the time is less than 60 seconds and your code hasn't run yet...
if(millis() < autoEndTime && !autonomousHasRun) {
// put all autonomous code below:
autonomousHasRun = true;
}
// if your autonomous code has run already and there is still time left...
else if(millis() < autoEndTime) {
// stop all motors
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
}
// if a button has been pressed and it has been less than 2 minutes since time started...
else if(IrReceiver.decode() && millis() < driverEndTime) {
unsigned long irCode = IrReceiver.decodedIRData.decodedRawData;
if(blueTeam) {
blueRemote(irCode);
}
else {
redRemote(irCode);
}
IrReceiver.resume(); // Receive the next value
}
// if no button has been pressed...
else {
delay(200);
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
}
}
// returns the microseconds number equal to the grabber degrees you input
int servoMicroseconds(int degrees) {
return map(degrees, 0, 180, 1000, 2000);
}
void blueRemote(int code) {
Serial.println(code);
// if the "1" button is pressed
if(code == 4097 || code == 6145 || code == 1 || code == 2049) {
open();
}
// if the "2" button is pressed
else if(code == 4098 || code == 6146 || code == 2 || code == 2050) {
close();
}
// if the "3" button is pressed
else if(code == 4099 || code == 6147 || code == 3 || code == 2051) {
raise();
}
// if the "5" button is pressed
else if(code == 4101 || code == 6149 || code == 5 || code == 2053) {
resetGrabber();
}
// if the left button is pressed
if(code == 4113 || code == 6161 || code == 17 || code == 2065) {
servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(1400);
}
// if the forward button is pressed
else if(code == 4128 || code == 6176 || code == 32 || code == 2080) {
servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(1600);
}
// if the right button is pressed
else if(code == 4112 || code == 6160 || code == 16 || code == 2064) {
servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1600);
}
// if the back button is pressed
else if(code == 4129 || code == 6177 || code == 33 || code == 2081) {
servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1400);
}
}
void redRemote(int code) {
Serial.println(code);
// if the "1" button is pressed
if(code == 8225 || code == 24513) {
open();
}
// if the "2" button is pressed
else if(code == 8257 || code == 24481) {
close();
}
// if the "3" button is pressed
else if(code == 8289 || code == 24449) {
raise();
}
// if the "5" button is pressed
else if(code == 8353 || code == 24385) {
resetGrabber();
}
// if the left button is pressed
if(code == 8865 || code == 23873) {
servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(1400);
}
// if the forward button is pressed
else if(code == 8737 || code == 24001) {
servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(1600);
}
// if the right button is pressed
else if(code == 8833 || code == 23905) {
servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1600);
}
// if the back button is pressed
else if(code == 8769 || code == 23969) {
servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1400);
}
}
// reset the servo so you can put the servo horn back on if it has been removed
void resetGrabber() {
grabber.writeMicroseconds(servoMicroseconds(90));
}
// open the grabber
void open() {
grabber.writeMicroseconds(servoMicroseconds(175));
}
// close the grabber
void close() {
grabber.writeMicroseconds(servoMicroseconds(80));
}
// raise the closed grabber
void raise() {
grabber.writeMicroseconds(servoMicroseconds(35));
}
Have you verified that pin 2 is still functioning, using a simple test sketch?
No, not specifically, but what I have done is test 4 different arduino-based robots that were not working before. I changed the IR receiver pin from 2 to 5 on all four of the robots and they then began working. Will test pin 2 though soon.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.