Arduino Serial connection

arduinoserial

I've written a small program for Arduino, in which it receives a two digit number from serial, for example ""26". Then it set's HIGH voltage to pin 2 with the duration of 6*100. That works fine, except that I also have a LED connected to pin 4, and it's voltage is set to HIGH at start-up, but when Arduino is receiving/sending through serial, it drops the voltage – is that normal?

The code is as follows:

int pts = 4;

String input;

void flash(int pin, int duration){
  Serial.println(pin);
  Serial.println(duration);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
  delay(duration);
  digitalWrite(pin, LOW);
}

void setup() {
  input="";
  digitalWrite(pts, HIGH);
  pinMode(pts,OUTPUT);
  Serial.begin(9600);
}

void parseInput(){
  if(Serial.available()>0){
//digitalWrite(led, LOW);
char incomingByte=(char)Serial.read(); 
input+=incomingByte;
delay(10);
  }
  else{

if(input!="")
{
  flash(input[0]-'0', (input[1]-'0') *100);
}
input=""; 
  }
}

void loop()
{
  parseInput();     
}

Best Answer

Have you tried moving the pinMode in the setup from below the digitalWrite to above it? This could cause the strange behaviour.

pinMode(pts,OUTPUT); 
digitalWrite(pts, HIGH);