SPI Not Working Running Only Once Or Twice.

Why is my code not working it runs max twice then freezed indefinately

#include <SPI.h>
bool done=false;

void setup()
{
pinMode(SS,OUTPUT);
Serial.begin(115200);
Serial.println(“Starting Test…”);
digitalWrite(SS, HIGH);
SPI.beginTransaction (SPISettings (4000000, MSBFIRST, SPI_MODE0));
}

void loop ()
{
Send2SPI(“AE16|\n”);
Reci3SPI();

}

String Send2SPI(String S)
{

int p=0;
digitalWrite(SS, LOW);
while(S[p]!=’\n’) {
char c=S[p];
SPI.transfer(c);
Serial.print(c);
p++;
}
Serial.println(" : Done…");
digitalWrite(SS, HIGH);
}

String Reci3SPI()
{
long int time = millis();
Serial.println(“Now Waiting For 2 Sec…”);
while((time+2000)>millis());
Serial.println(“Resuming…”);
return “0”;
}

In your setup function,

// change this
  SPI.beginTransaction (SPISettings (4000000, MSBFIRST, SPI_MODE0));
// to this
  SPI.begin();

Already Tried Both using 1. begin(); 2. beginTransaction(....); 3.beginTransaction(....); followed by begin(); with no avail.

Which Arduino are you using? What pin are you using for the SPI slave select?

<code>

You need square brackets to make code tags :slight_smile:
You can edit your original message and fix them up.

Pete

Add SPI.begin ().

Get rid of String.

ashish_13:
Already Tried Both
using

  1. begin();
  2. beginTransaction(…);
    3.beginTransaction(…); followed by begin();
    with no avail.

Show your code with SPI.begin () and use code tags please.

http://gammon.com.au/spi

Already Tried SPI.begin() & AS For the Square Brackets That Definately Wont Make Difference I Edited A Code Eariler With Hope That Somebody Will Help But No One Did So Its A Waste Of Time.

AS For The Hardware Want To Interface ESP8266 ESP-12 With ATMEGA2560 / ATMEGA328 Cant Use I2C As Data Is 84Bytes And One More Device Is On I2C BUS Which Requires Time To Process

Not a waste of time. Here is your code fixed. This is an example of why I don’t use the String data type. That is all I changed.

#include <SPI.h>
bool done = false;

void setup()
{
  pinMode(SS, OUTPUT);
  Serial.begin(115200);
  Serial.println("Starting Test....");
  digitalWrite(SS, HIGH);
  SPI.begin();
}

void loop ()
{
  Send2SPI("AE16|\n");
  Reci3SPI();

}

char* Send2SPI(char* S)
{

  int p = 0;
  digitalWrite(SS, LOW);
  while (S[p] != '\n') {
    char c = S[p];
    SPI.transfer(c);
    Serial.print(c);
    p++;
  }
  Serial.println(" : Done...");
  digitalWrite(SS, HIGH);
}

char* Reci3SPI()
{
  long int time = millis();
  Serial.println("Now Waiting For 2 Sec...");
  while ((time + 2000) > millis());
  Serial.println("Resuming....");
  return "0";
}

edit: That return value from Reci3SPI may cause problems.

Thanks Will Try In Few Hours

It doesn't freeze. I can't guarantee it will work with the hardware you are using.

Please don’t capitalize every word. It is hard to read. It makes the sentence look like a very long book title.

ashish_13: Already Tried SPI.begin()

Show your code using SPI.begin ().

I trust I don't have to repeat myself again.