I am trying to send ADS 1115 data which is connected with Arduino mega_1 over I2C and arduino Mega_1 is connected with arduino Mega_0 over SPI. Arduino Mega_1 is act as a slave and send ADS 1115 data to master device.. I think my whole setup is working but i want to received actual data of ADS 1115 on master Arduino Here is screenshots of results of my code
master
#include <SPI.h>
void setup (void)
{
Serial.begin (9600);
Serial.println ();
digitalWrite(SS, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
} // end of setup
byte transferAndWait (const byte what)
{
byte a = SPI.transfer (what);
delayMicroseconds (20);
return a;
} // end of transferAndWait
void loop (void)
{
byte a;
// enable Slave Select
digitalWrite(SS, LOW);
transferAndWait ('a'); // add command
a=transferAndWait (0);
// disable Slave Select
digitalWrite(SS, HIGH);
Serial.println ("results:");
Serial.println (a);
delay (1000); // 1 second delay
} // end of loop
slave
#include <Wire.h>
#include <Adafruit_ADS1015.h>
int out_buf;
int adc0;
// Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
Adafruit_ADS1015 ads;
// what to do with incoming data
volatile byte command = 0;
void ss_falling ()
{
command = 0;
}
void setup (void)
{
ads.begin();
Serial.begin(9600);
//ads.startComparator_SingleEnded(0, 1000);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
attachInterrupt (0, ss_falling, FALLING);
}
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR;
switch (command)
{
// no command? then this is the command
case 0:
command = c;
SPDR = 0;
break;
// add to incoming byte, return result
case 'a':
SPDR = out_buf; // add 15
break;
} // end of switch
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
{
adc0 = ads.readADC_SingleEnded(0);
Serial.print("AIN0: "); Serial.println(adc0);
out_buf=adc0;
//int buff = atoi(out_buf);
Serial.print("AIN1: "); Serial.println(out_buf);
delay(1000);
}
i am sending 1570 but on Master side i received 35 i think ADS data is of 16 bits i.e 2 bytes and SPI received only 1 byte at a time i am stuck here please help me. Thank you in Advance