I'm working on a key-pad sort of project (just messing around) and whenever I upload the code and open up the Serial monitor, I get a whole bunch of P's and @ signs. Both the monitor and the arduino are on the same sample speed (9600) and I just have no idea what's going on. It could be because I'm printing a character rather than a number but I'm not sure, please help! Here's my code:
int current;
int pins[]={1,2,3};
int count=3;
void setup()
{
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
Serial.begin(9600);
}
void loop()
{
for (current=0;current<count;current++)
{
digitalWrite(pins[current],HIGH);
digitalWrite(pins[current],LOW);
}
if (digitalRead(1)==HIGH && digitalRead(4)==HIGH)
{
Serial.println("1");
}
if (digitalRead(2)==HIGH && digitalRead(4)==HIGH)
{
Serial.println("2");
}
if (digitalRead(3)==HIGH && digitalRead(4)==HIGH)
{
Serial.println("3");
}
if (digitalRead(1)==HIGH && digitalRead(5)==HIGH)
{
Serial.println("4");
}
if (digitalRead(2)==HIGH && digitalRead(5)==HIGH)
{
Serial.println("5");
}
if (digitalRead(3)==HIGH && digitalRead(5)==HIGH)
{
Serial.println("6");
}
if (digitalRead(1)==HIGH && digitalRead(6)==HIGH)
{
Serial.println("7");
}
if (digitalRead(2)==HIGH && digitalRead(6)==HIGH)
{
Serial.println("8");
}
if (digitalRead(3)==HIGH && digitalRead(6)==HIGH)
{
Serial.println("9");
}
}
void loop()
{
for (current=0;current<count;current++)
{
digitalWrite(pins[current],HIGH);
digitalWrite(pins[current],LOW);
Looks to me like everytime through your loop() function you are 'stepping on top of' pins 0 and 1 which are the serial data pins. If you expect to use serial commands back to the PC you need to make sure you don't perform any digitalWrite() commands to pins 0 and 1.
With no delay between setting the pin HIGH and setting it LOW, what are you accomplishing?
if (digitalRead(1)==HIGH && digitalRead(4)==HIGH)
You're reading from an OUTPUT pin that you have set LOW. You don't really expect to read HIGH do you?
More importantly I think, leaving that code with pin 1 at a LOW state is putting a 'break' condition (or start condition depending on the timing) of the serial link back to the PC, so sure to upset the next serial output command.