Author Options:

Arduino code help input binary to pin 4? Answered

unsigned char APin=4;
unsigned char BPin=5;
unsigned char CPin=6;
unsigned char PinState;
unsigned char LastPinState=B111;  // As in High, High, High for random mode

pinMode(APin, INPUT);
  pinMode(BPin, INPUT);
  pinMode(CPin, INPUT);
  digitalWrite(APin, HIGH);
  digitalWrite(BPin, HIGH);
  digitalWrite(CPin, HIGH);

this code im using is outdated for me now and im using an iphone to send binary commands to atmel328 chip witch tells sends the binary code out a pin to my arduino can i recieve a b101 or b111 on pin 4 or any other pin. how do i change what i have right now it reads the state of pins 4,5,6 aka a,b,c and displays different messages. so if i ground b and c it plays one of the messages and so on i dont want this i want to take the b001 being sent from my other boards and use it on pin 4 aka a

void loop(){
  PinState = B111;       // Read ABC and assign to a 3 bit number
  if(digitalRead(CPin)==LOW){        // A if Gnd -1 to make B??0
    PinState = PinState - 1;                  
  if(digitalRead(BPin)==LOW){        // B if Gnd -2 to make B?0?
    PinState = PinState - 2;   
  if(digitalRead(APin)==LOW){        // C if Gnd -4 to make B0??
    PinState = PinState - 4;   
  if (PinState != LastPinState){
    LastPinState = PinState;
    switch (PinState){
      case B000:{    // B000  HPs all on random on/off
        if((FrontHPStateDefault==0) && (RearHPStateDefault==0) && (TopHPStateDefault==0)){
        } else {
      case B001:{    // B001 Cantina MP3
        FeedString    (1, FrontHPStateDefault, RearHPStateDefault, TopHPStateDefault, 1,0, 0, "<<<<<<<<<");
      case B010:{    // B010 Han Shot First
        FeedString    (8, 3,3,3, 1,1, 1, " HAN SHOT FIRST! ");
      case B011:{    // B011 Short Circuit
        Failure       (7, 5,5,5, 8);
        LogicOffDelay (6, 0,0,0, 1, 20);
        FailureReverse(8, 4,4,4, 2);



6 years ago

Pin 4 is only going to be able to read if there is 5V coming to it or 0 volts. So if you are trying to use your iphone to send a signal then the iphone needs some way to either pull the pin up to 5V or down to 0(gnd). Your code will need to track how many time the pin state changes.

I think what you need to do is look into having the iphone send a serial command to the serial pins of the arduino.


Answer 6 years ago

Is "pulling it up" wise ? Better to use a signal translator methinks.


Answer 6 years ago

thanks mpilchfamily : would you be able to show me an example that would work with my code (like case b010 han shot first is a led sequence. right now i ground 2 pins to get it to come up. i can send any command from my iphone so thats not a problem for me but what pin is the serial on the pro mini and how do i pull it up. thanks for your time.


Answer 6 years ago

Start simple. Play around with the examples for serial communication with the PC. Where you input your desired string and it gets sent to the Arduino. When the arduino receives the string it can make the proper comparisons and call for whatever actions you want taken.

Look up your arduino and see what pins do what. Start learning about serial communication with the arduino and you'll get it figured out. (hint: look at the RX and TX pins)