Hey guys,
I have this NC switch (as part of my project) and I need it to toggle a port.
Now I have made the pin as an INPUT and written high it it to activate the pullup
But, when the switch/wire is connected from the pin to ground ( as its supposed to), which means
the pin is 0V, the program doesnt run.
When I remove the switch, the program runs.
Does this have something to do with the pin stuff not being defined before the pin is grounded, making an internal short in the Arduino?
Any way around this except get new NO switches or activate the switches as the Arduino starts up?
Code:
//28 is L, 34 is R, 18 is B
//pin 7 is ground for some reason
//pins 6 and 5 are connected to NC switch to ground, calibrate buttons 6=H, 5=L
//buttons 1 and 2 pins are 9 and 10, they connect to button switchs on joystick
long received=0; //number value of received
long num=0; //between number, multiplier by 10
long fin=0; //final number
byte countnum=4; //number of output digits, 5 digits max, value =4
int count=countnum; //count for power of 10
byte btnflag,btncount=0; //are we on button part of string
byte btn1p=9; //button1 pin is 9
byte btn2p=10; //button2 pin is 10
byte pinhi=6; //input pin for calibration hi
byte pinlo=5; //in pin for calib low
byte Ranalogp=11; //PWM output
byte Lanalogp=10;
long maxvalr=500; //calibration values
long maxvall=500;
long minvalr=0;
long minvall=0;
long ranger=maxvalr-minvalr; //range of values
long rangel=maxvall-minvall;
void setup() //useless for now
{
Serial.begin(9600); //baudrate-- MAKE SURE TO CHANGE WHEN DOING WIRELESS
pinMode(pinhi,INPUT); //NC button will be connected to ground
pinMode(pinlo, INPUT);
pinMode(Ranalogp, OUTPUT);
pinMode(Lanalogp, OUTPUT);
digitalWrite(pinhi, HIGH); // turn on pullup resistors
digitalWrite(pinlo, HIGH); // turn on pullup resistors
}
void loop()
{
if (Serial.available() > 0) { //if theres a serial in
// read the incoming byte:
received = ((int)Serial.read()-48); //capture and convert to int, get real value
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//button stuff
if (received == 18) //set flag if B detected
{
btnflag=1;
}
if(btnflag==1 && btncount==1) //if b string to be read
{
if(received==1) //if value for b1 is 1
{
digitalWrite(btn1p,HIGH);
}
else
{
digitalWrite(btn1p,LOW); //if value for b1 is 0, set out to low
}
}
if(btnflag==1 && btncount==2)
{
if(received==1)
{
digitalWrite(btn2p,HIGH);
}
else
{
digitalWrite(btn2p,LOW);
}
}
btncount++; //counts up to 3
//1 for B
if(btncount==3) //1 for b1
{btnflag=0;} //2 for b2
//////////////////////////////////////////////////////////////////////////////////////////////////
//formulate into number
if (btnflag==0)
{
btncount=0; //clear count
if(received!=28 && received!=34 && received!=18) //if not either R or L
{
num = (pow(10, count)+0.1)*received; //scale value
count--;
fin+=num; //put values together for 1 number
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//gather an output
//----------------------------
if(received==34) //if it was R
{
Serial.print('L'); //then print L because value was before
//-------------
if(digitalRead (pinhi) == HIGH)
{
maxvall=fin;
}
if(digitalRead (pinlo) == HIGH)
{
minvall=fin;
}
rangel=maxvall-minvall;
fin-=minvall;
//---------------
fin=(float)fin/rangel*256;
if(fin<0)
{fin=0;}
if(fin>255)
{fin=255;}
analogWrite (Lanalogp, fin);
Serial.println(fin); //PWM out/scale here
fin=0; //reset count and fin
count=countnum;
}
//-----------------------------
if(received==28) //if was L then print R b/c values before L
{
Serial.print('R');
/////////////
if(digitalRead (pinhi) == HIGH)
{
maxvalr=fin;
}
if(digitalRead (pinlo) == HIGH)
{
minvalr=fin;
}
ranger=maxvalr-minvalr;
fin-=minvalr;
///////////////
fin=(float)fin/ranger*256;
if(fin<0)
{fin=0;}
if(fin>255)
{fin=255;}
analogWrite (Ranalogp, fin);
Serial.println(fin);
fin=0;
count=countnum;
}
//----------------------------
}//end buttonflag
}//end serial available
}
EDIT: sry, fixd a few mistakes
EDIT2: since I posted the program here's a rundown from my second post:
basically what the program does is decode a serial input string like:
R00000L00000B00
Makes values and outputs it to PWM
I need to scale those values to 256.
Now, when uploading the program I remove the serial rx from the other arduino.
When I click serial port, it brings up the window and no output.
To get an output I must reset the arduino after taking out the shorts (in place of switches) to ground and then put them back for original function.
EDIT3: Final code posted
Thanks