0
\$\begingroup\$

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();     
}
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 19, 2014 at 15:03
  • 2
    \$\begingroup\$ This is arduino, there's no main \$\endgroup\$
    – Dzarda
    Apr 19, 2014 at 15:08
  • 1
    \$\begingroup\$ @MustSeeMelons Just FYI. There is now a stack dedicated to Arduino arduino.stackexchange.com \$\endgroup\$ Apr 19, 2014 at 15:18
  • \$\begingroup\$ So what's the application entry point exactly? I really don't understand arduino :( \$\endgroup\$ Apr 19, 2014 at 15:28
  • 1
    \$\begingroup\$ 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). \$\endgroup\$
    – Dave Tweed
    Apr 19, 2014 at 16:01

1 Answer 1

1
\$\begingroup\$

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);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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