Shoryuken

Connecting buttons in analog header

6 posts in this topic

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Oh, I just noticed that there needs to be a change in your analog.read(x) call. With our implementation analog.read(x) will read the channel instead of the pin. Since you are connecting to channel 0 of ADC1 then you will want to do: analog.read(0);

Share this post


Link to post
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.

Share this post


Link to post
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;  }}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now