Trouble with 74HC165N shift register readings


#1

Hi everyone,

I’ve recently been writing an mBlock3 extension for a custom Arduino Shield I made, this shield is using a shift register (parallel to serie) to read inputs from switches and I’m having trouble reading those using the register in mBlock.

I want to be able to use only one block to read, in Scratch Mode, a specific input from a switch received by the shift register.
For example if the switch number 2 is activated, a signal will be sent to pin n°2 on the register, which i’m gonna want to read, so i will shift the bits until the information of the n°2 pin is send to the output arduino pin.

With the following bit of code i can read the outputs correctly in the Arduino IDE :

byte read_Registers()
{
byte received;

digitalWrite(LOAD, LOW);
digitalWrite(CLOCK, HIGH);
delayMicroseconds(5);
digitalWrite(LOAD, HIGH);
delayMicroseconds(5);

received = shiftIn(DATA, CLOCK, LSBFIRST);

/for (int i = 0; i<8 ; i++) {
received = received | (digitalRead(DATA) << i);
digitalWrite(CLOCK, HIGH);
digitalWrite(CLOCK, LOW);
}
/

digitalWrite(LOAD, LOW); //send data

return received;
}

But I can’t replicate this bit in the javascript part of the extension files, to make it work. Wether I need to activate the block twice to have a correct reading or i need to create two blocks (one shifting the bit to the output pin, and the other one reading that pin) to have a correct reading.

Though I want to use only one block, to shift and read.

Here is the javascript extension code of the block that needs to be activated twice to get a correct reading :

ext.getBinaryIn = function(nextID, entree_binaire_select)
{
var deviceId = 30;

getPackage(nextID,deviceId,pins_reg[0]); //reading DATA

runPackage(30, pins_reg[1], 0); //digitalWrite(LOAD, LOW);
runPackage(30, pins_reg[2], 1); //digitalWrite(CLOCK, HIGH);

 waitMilliseconds(0.005);

 runPackage(30, pins_reg[1], 1); //digitalWrite(LOAD, HIGH);

 waitMilliseconds(0.005);

  for (var i = 0 ; i<entrees_binaires[entree_binaire_select] ; ++i)
  {
    runPackage(30, pins_reg[2], 1); //digitalWrite(CLOCK, HIGH);
    runPackage(30, pins_reg[2], 0); //digitalWrite(CLOCK, LOW);
  }

};

My guess is that as I’m writing in a “reading” block, it creates some kind of conflict when the function is called.

So my questions are :

Is it even possible to write and read in the same block ?
If it is what am I doing wrong ?
Do you have any tips or ideas on how to do this ?

Thanks a lot,

RmnRss


#2

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.