1

I want two Arduino cards to communicate with each other using SPI and move the servo motor with potentiometer as a result. I've done the pre-burning led or the led burning led on the SPI, I am waiting for your help. I will give the code below.

MASTER Arduino code

#include <SPI.h>
/* SPI haberleşmesinde Master olarak görev yapan Arduino kodu */
const int pot=A0; // Servo kontrol potunu bağlayacağımız analog giriş.
int potdurum=0; 
int pos = 0; 
const int SSpini = 8; 
/* Slave görevindeki Arduino'nun seçilmesi için 8 bumaralı pin SS olarak belirlendi */
void setup()
{
 Serial.begin(9600);
 pinMode(SSpini, OUTPUT);
 /* SS pini çıkış olarak ayarlandı */
 digitalWrite(SSpini, HIGH);
 /* Slave olan Arduino başlangıçta haberleşmeye geçmemesi için SS pini HIGH yapıldı */
 SPI.begin();
 /* SPI haberleşmesi başlatıldı */
}
void loop()
{
 potdurum=analogRead(pot);
 pos= map(potdurum,0,1023,0,180);//Pottan okunan değeri 180e oranlıyoruz.
 veriGonder(pos,SSpini);
}
/* SPI üzerinden veri göndermek için yazılmış fonksiyondur */
void veriGonder(int veri, int SSpini)
{
 digitalWrite(SSpini, LOW);
 /* Diğer Arduino'nun veri dinlemeye başlaması için SS hattını LOW düzeyine çekmeliyiz */
 delay(1);
 /* Kısa bir süre bekleyelim */
 Serial.print("Aci Degeri = ");
 Serial.println(veri);
 delay(15);
 SPI.transfer(veri);
 /* veri karakteri diğer Arduino'ya yollandı
 * Fonksiyon diğer arduino'dan gelen veriyi vermektedir
 * Geri gelen veri LEDDurumu değişkeninde tutulmuştur
 * Bu değişken Seri port üzerinden bilgisayardaki Serial monitöre yollanmıştır
 */
 digitalWrite(SSpini, HIGH);
 /* Haberleşmeyi bitirmek için pin LOW konumuna çekilmiştir */
}

SLAVE Arduino code

#include <Servo.h>
int i = 0;
Servo myservomotor;
void setup()
{
 Serial.begin(115200);
 myservomotor.attach(3);
 pinMode(MISO, OUTPUT);
 pinMode(SS , INPUT);
 /* MISO pini OUTPUT, SS pini INPUT olarak ayarlandı */
 SPCR |= 0b11000000;
 SPSR |= 0b00000000;
 /* Slave mod ve interrupt için ayarlamalar yapıldı */ 
 sei();
 /* Interruptlar çalıştırıldı */
} 
void loop()
{
}
/* SPI kesmesi olduğunda çalışacak fonksiyon */
ISR(SPI_STC_vect)
{
 cli();
 /* Interruptlar durdurulmuştur */
 while(!digitalRead(SS))
 {
 SPDR = i;
 i ++;
 if ( i > 255) 
 i = 0;
 while(!(SPSR & (1 << SPIF)));
 /* SPI hattında veri aktarımı bitene kadar bekle */
 int gelenVeri;
 gelenVeri = SPDR;
 myservomotor.write(gelenVeri);
 Serial.print("Aci Degeri Slave= ");
 Serial.println(gelenVeri);
 delay(15);
// if(gelenVeri == 'a'){
// digitalWrite(SERVO,HIGH);
// }else if(gelenVeri == 'b'){
// digitalWrite(SERVO,LOW); 
// }
 /* SPI üzerinden gelen verinin değerine göre LED'in konumu değiştirildi */
 }
 sei();
 /* Interruptlar başlatılmıştır */
}

enter image description here

asked Jul 21, 2017 at 7:12
4
  • 5
    What's your question? Commented Jul 21, 2017 at 7:35
  • 1
    The basic error is too much code in the SPI ISR. Focus on capturing the SPI value and then flag to the loop() function that a new value is available. Avoid delay and serial print in ISRs. Commented Jul 21, 2017 at 20:26
  • @LookAlterno - this site requires posts and comments to be in English, thanks. Commented Jul 22, 2017 at 7:04
  • I am waiting for your help. - for what, exactly? Commented Jul 22, 2017 at 7:07

1 Answer 1

2
/* SPI kesmesi olduğunda çalışacak fonksiyon */
ISR(SPI_STC_vect)
{
 cli();
 ... 
 Serial.print("Aci Degeri Slave= ");
 Serial.println(gelenVeri);

Don't do serial prints inside an ISR.

answered Jul 22, 2017 at 7:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.