1

Can anyone help me with this issue.

We created an arduino project. We listen microswitch. If switch click, Arduino send basic serial data. And C# application read serial port.

This solution is working but after more times (3Hours, 5Hours,..) system stoped. Error message is "a device attached to the system is not functioning" Solution: Usb remove and retry plug system is going to running.

We are try putty.exe maybe problem C# project but don't working.

If microswitch click don't flash "TX" or "RX" led.

const int button = 3;

uint64_t bounceTime = 50;
uint64_t holdTime = 250;
uint64_t doubleTime = 500;

uint64_t lastReading = LOW;
uint64_t hold = 0;
uint64_t single = 0;

uint64_t onTime = 0;
uint64_t lastSwitchTime = 0;


void setup() {
 pinMode(button, INPUT_PULLUP);
 Serial.begin(9600);
}

void loop() {

 int reading = digitalRead(button);

 if (reading == HIGH && lastReading == LOW) {
   onTime = millis();
 }

 if (reading == HIGH && lastReading == HIGH) {
   if ((millis() - onTime) > holdTime) {  
     hold = 1;
   }
 }

 if (reading == LOW && lastReading == HIGH) {
   if (((millis() - onTime) > bounceTime) && hold != 1) {
     onRelease();
   }
   if (hold == 1) {
      delay(5000);
      reading = digitalRead(button);
      if(reading==LOW){
              Serial.println("B");
      }
     hold = 0;
   }  
 }
 lastReading = reading;

 if (single == 1 && (millis() - lastSwitchTime) > doubleTime) {
   single = 0;
 }

}

void onRelease() {
 if ((millis() - lastSwitchTime) >= doubleTime) {
   single = 1;
   lastSwitchTime = millis();
   return;
 }  

 if ((millis() - lastSwitchTime) < doubleTime) {
   single = 0;
   lastSwitchTime = millis();
 }  

}
  • Why not uint32_t? – Mikael Patel May 6 '16 at 16:07
  • 1
    @MikaelPatel irrelevant – JayEye May 6 '16 at 17:36
1

I assume that by "USB" you mean "USB-to-serial adaptor".

Try running the same code without any serial communications, just have an LED do the equivalent of printing, and power it from a good power supply. If it runs for more than five hours, the problem is with the USB adaptor and/or your PC (Windows XP? Really?), and hence not relevant to this forum.

If the code crashes, then maybe you have a marginal board, or there is some other environmental factor that's giving you trouble. The code looks OK on first glance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.