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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.