Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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();     
}
share|improve this question
    
Is it possible that PIN4 is used by the serial as txd or rxd? If it's so the led might dim but only during transmission or reception. And you are missing the main() function. –  Vladimir Cravero Apr 19 at 15:03
2  
This is arduino, there's no main –  Dzarda Apr 19 at 15:08
1  
@MustSeeMelons Just FYI. There is now a stack dedicated to Arduino arduino.stackexchange.com –  Nick Alexeev Apr 19 at 15:18
    
So what's the application entry point exactly? I really don't understand arduino :( –  Vladimir Cravero Apr 19 at 15:28
1  
In Arduino, the main() is part of the library code. The key concept is that it calls setup() once and then calls loop() repeatedly (while also doing other internal functions). –  Dave Tweed Apr 19 at 16:01

1 Answer 1

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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