enter image description hereWhat to add on this code to check my pins state during run? And how can i trigger error using Visual basic when 1 of my pin is disconnected?
String input = "";
void setup(){
Serial.begin(9600); // Sets up communication with the serial port
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT); // for start of program operation
pinMode(12,OUTPUT); // for stop of program operation
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
void loop(){
int recipe;
//Serial.print("Enter recipe number: ");
while(Serial.available()==0){ // Waits for data from serial port
}
while(Serial.available() > 0){
int data = Serial.read();
// convert the incoming byte to a char
// and add it to the string
input += (char)data;
recipe = input.toInt()+'0'; //recipe=0 if not a digit
if(recipe == '0')
recipe = data;
if(data == '\n'){
//Serial.print("Recipe: ");
//Serial.println(recipe);
//Serial.print("String: ");
//Serial.println(input);
input = "";
}
}
if(recipe == 's'){
digitalWrite(11,HIGH);
delay(1000);
digitalWrite(11,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
}
else if(recipe == 't'){
digitalWrite(12,HIGH);
delay(1000);
digitalWrite(12,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
}
else if(recipe>'0' && recipe<'32'){
recipe = recipe-'0';
char binary[7] = {0}; // This is where the binary representation will be stored
recipe += 32; // Adding 32 so that there will always be 6 digits in the string
itoa(recipe,binary,2); // Convert recipe to a string of base 2 and save it in array 'binary'
char* signals = binary + 1; // Get rid of the most significant digit to get the 5 bits
//Serial.print(signals); // Print out the signals in binary
int i;
for(i=0; i<5; i++){
if(signals[i] == '0')
signals[i] = '1';
else
signals[i] = '0';
digitalWrite(i+6, signals[i]-'0'); // write to pin; converts the bit of the string to HIGH or LOW
//Serial.print(signals[i]);
}
}
else
loop();
}
-
Welcome to Stackexchange. Please ask a question. And please post your code in a readable format (see editor help).sekdiy– sekdiy2016年03月24日 06:19:14 +00:00Commented Mar 24, 2016 at 6:19
-
It's probably not possible to know how an interrupted signal can be recognised in code as long as we don't know how you connect them. Without external logic only input pins could do this in code and even then Arduino has a lot of them.sekdiy– sekdiy2016年03月24日 06:38:21 +00:00Commented Mar 24, 2016 at 6:38
-
I have attached the connections of my pins to the tool controller. The purpose of this setup is Arduino will send the data from my traveller to my tool then automatically start my tool once data was received. Since the data from my traveller is critical and Arduino must send the correct value to the tool prior start. The only thing i need to know is that how can i detect if my pins are connected properlyQueen Quiazon– Queen Quiazon2016年03月24日 06:42:46 +00:00Commented Mar 24, 2016 at 6:42
-
If a pin gets disconnected, Arduino won't recognise it as is. This would require some form of feedback from the tool controler, e.g. by bringing another set of wires from the connection terminals back to some of Arduino's input pins. Also, your code still contains errors (e.g. you call loop() from within loop()). And then there's Visual Basic all of a sudden. :) May I suggest you compile this into different questions?sekdiy– sekdiy2016年03月24日 06:53:08 +00:00Commented Mar 24, 2016 at 6:53
1 Answer 1
As mentioned in a comment, your code has some fundamental flaws. Do not call loop()
from within loop()
! Aside from the code errors, your question has a legitimate element.
Testing the tri-state behaviour of an input pin will let you know if it is genuinely high, genuinely low, or floating (disconnected). The technique was given in this answer to similar problem I was encountering.
In short:
Connect a large (1MOhm) resistor to each input between the pin and ground, then for each pin:
- Turn off internal pullup;
- Read value of input pin;
- Turn on internal pullup;
- Read value again.
If the value changes from the first to the second digitalRead()
, the input was not connected to anything. If the value remains the same then it is a genuine value from a connected device.
-
+1, Great answer to the original issue of detecting an open pin. It's also in line with my suspicion that external hardware is required: feedback to input pins... ;)sekdiy– sekdiy2016年03月24日 08:53:36 +00:00Commented Mar 24, 2016 at 8:53
-
That being said, what about shorts to ground or supply, what if the impedance in the machine is too low for this approach... I have a suspicion that this arduino shall double as a failsafe device in order to protect the machine and people – and that involves much more.sekdiy– sekdiy2016年03月24日 08:59:47 +00:00Commented Mar 24, 2016 at 8:59
-
1If it is for a truly safety-critical application then there should be many more steps taken, the first being a second processor (complete Arduino board) for a Master/Slave setup. Then there's 'toggle-checking' of all the inputs as the next step. However, if the OP posted this question in the first place then they should not be responsible for designing something so technical.CharlieHanson– CharlieHanson2016年03月24日 09:05:43 +00:00Commented Mar 24, 2016 at 9:05
Explore related questions
See similar questions with these tags.