Why do I need to enable the serial interface for my program to work?

Hi All,

I'm very new to Arduino and have made a simple light control program using interrupts. The code refuses to work if I don't enable the serial connection AND print out the value of claps. The code works without the USB plugged in. Obviously I can't really tell what is going on when I remove the serial interface lines!

Any ideas?

I have marked the required lines with //why do I need this line?

#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define SOUND 2
#define ANALOG 0
 
#define FADESPEED 10     // make this higher to slow down
int LEDon=LOW;
int bright;
volatile int claps = 0;

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(SOUND, INPUT);
    Serial.begin(9600); // why do I need this line?
    attachInterrupt(digitalPinToInterrupt(SOUND), ClapReg, RISING);
}
 
 
void loop() {

bright = analogRead(ANALOG);

if(LEDon == HIGH){
  analogWrite(REDPIN,bright/4);
}
else{
  analogWrite(REDPIN,0);
}
Serial.println(claps);  // why do I need this line?

if (claps >= 2 ){
    LEDtoggle();
    claps = 0;
    }
}
   
void LEDtoggle(){
if (LEDon == LOW){
  
  LEDon = HIGH;
}
else{
 
  LEDon = LOW;
}
  
}
void ClapReg(){
  static unsigned long last_interrupt = 0;
  unsigned long this_interrupt = millis();

  if (this_interrupt - last_interrupt > 200){
   claps++;
   last_interrupt = this_interrupt;
  }
}

In the absence of any real information I am assuming you have a Uno.

Serial.println(claps);  // why do I need this line?

You don't.

Serial.begin(9600); // why do I need this line?

You don't but without this, anything written to the serial port will eventually stall the processor waiting for the buffer to empty.

Hi Mike,

Thank you for your response. Apologies I should have specified I have an Uno R3

I know that I should not need these lines.

With the lines in the code does what I expect and the light blinks on and off if I clap twice.

If I remove the lines (without changing anything else) the light no longer toggles no matter how much I clap.

I can’t see a sensible reason why the code shouldn’t work fine with the lines removed…

With further experimentation if I only remove the line Serial.println(claps); then the code fails to turn the LED off and on.

I wonder if I need to use the variable claps within the main loop before the if statement can read it correctly?

the code fails to turn the LED off and on.

What LED is this then.
What schematic do you have?

You are missing something that you have not told us about.

Try replacing your print statement with a delay(10)

last_interrupt needs to be declared volatile.

 static volatile unsigned long last_interrupt = 0;

Pete

Hi Mike,

I have an RGB led connected (through transistor drivers) to pins 3 5 and 6. REDPIN is the Red led pin.

I have a digital output from a sound sensor connected to pin 2 with a 10k pulldown resistor.

I have further investigated the interrupt problem with an even simpler piece of code

#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define SOUND 2

void setup() {
  // put your setup code here, to run once:
pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(SOUND, INPUT);
  attachInterrupt(digitalPinToInterrupt(SOUND), ClapReg, RISING);
}

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

}

void ClapReg(){
static unsigned long last_interrupt = 0;
  unsigned long this_interrupt = millis();

  if (this_interrupt - last_interrupt > 200){
  digitalWrite(REDPIN,HIGH);
}
}

This code does not light the LED on a rising edge on the input pin so I don't believe the interrupt is working here either.

With serial enabled it works...

#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define SOUND 2

void setup() {
  // put your setup code here, to run once:
pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  pinMode(SOUND, INPUT);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(SOUND), ClapReg, CHANGE);
}

void loop() {Serial.println('a');
  // put your main code here, to run repeatedly:

}

void ClapReg(){
static unsigned long last_interrupt = 0;
  unsigned long this_interrupt = millis();

  if (this_interrupt - last_interrupt > 200){
  digitalWrite(REDPIN,HIGH);
}
}

It may be that I need a different pulldown resistor because I can get things to happen by touching the line to +5V and GND.

Updated that line thanks Pete. No effect on the problem.

@Mike

Thank you for your help. I think it is something to do with the incoming signal rather than the code as it works fine with a push button instead of the sound sensor.

... OK I'm closing this issue as removing the pulldown on the sound input pin fixed the problem. Really interesting that enabling serial caused the interrupt to trigger at a slightly lower voltage level.

Mike and Pete: thank you for your help and patience

Question - How long do you think 200 mills is?

Mark

If I remove the lines (without changing anything else) the light no longer toggles no matter how much I clap.

Does it flicker?

Basically your clap is lasting more than 200 mills (if fact at least twice that) so a single clap (without the print gives you a very quick on/off. Adding the serial print slows things down a little as a result the program does not see a “double” clap.

Try changing 200 to 2000.

Your debounce timer is ti short would be another way to put it.

Mark