Connecting buttons in analog header


Shoryuken

Recommended Posts

Hi,

 

I'd like to implement some buttons from DFRobot to my Retrocade Synth.

I naively believed that I could just connect the 3 wires to the analog input but maybe that's not that simple.

And I don't know which pin number I have to listen...  so I've tried every possible pin number but I always get "key = 0" even if I press a button.

 

Could someone help me ?

 

Please see my code and the wiring below.

 



#include "SPIADC.h"
#include "SPI.h"


int pin = 32;
int key_in = -1;
int last_key = -1;


void setup()
{
  Serial.begin(115200); 
  Serial.println("BEGINNING");  
}


void loop()
{
  key_in = analog.read(pin); 
  if (key_in != last_key) {
    Serial.print("key = ");
    Serial.println(key_in);
    last_key = key_in;
  }
}


 

 

 

P1020505_ngm3.JPG

Link to comment
Share on other sites

Hello,

 

It looks like you should be really close to having it work, the only thing that is missing is defining how the SPI ADC is connected, we need to tell the library which Wishbone slot we are using and what CS pin to use. 

 

Please try to add this to your setup function (this is for ADC1):

analog.begin(CS(WING_CH1),WISHBONESLOT(8),ADCBITS(SPIADC_12BIT));

Or, if you want to use ADC2:

analog.begin(CS(WING_CL5),WISHBONESLOT(6),ADCBITS(SPIADC_12BIT));

Please note that you can get this information by clicking the "View Schematic" icon in DesignLab when you have the RetroCade Synth project opened. This is assuming that you have the RetroCade Synth bit file loaded to the Papilio.

 

Let me know if this works. 

 

Jack.

Link to comment
Share on other sites

There is also an example in DesignLab that you can test with. If you go to the table of contents and search for the Analog SPI Wing project there is an existing code example for communicating with the SPI ADC. You will just need to load the RetroCade Synth bit file and change the analog.begin lines to match how the ADC is connected (like posted above).

 

Jack.

Link to comment
Share on other sites

Thanks a lot Jack ! Now it works  :D

 

Here's the full code :

 

#include "SPIADC.h"#include "SPI.h"int pin = 0;int key_in = -1;int last_key = -1;void setup(){  analog.begin(CS(WING_CH1),WISHBONESLOT(8),ADCBITS(SPIADC_12BIT));  Serial.begin(115200);   Serial.println("BEGINNING");  }void loop(){  key_in = analog.read(pin);   if (key_in != last_key) {    Serial.print("key = ");    Serial.println(key_in);    last_key = key_in;  }}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.