Hello,
2 Arduino Uno are connected by SPI (pins 10,11,12,13). Arduino#1 (master) is connected to the computer. On Arduino#2 (Slave) pin 2 is connected to a LED. What is the minimum program for controlling this LED on Arduino#2 from the computer only connected on Arduino#1 and receive a message: Led on or Led off?
Thank you very much
Jo
pylon
March 31, 2017, 4:45pm
2
Why are you using SPI?
Define which of the two should be the slave!
Minimalistic approach (I don't say it's the minimum and it's a quick hack, not tested):
Master:
#include "SPI.h"
void setup() {
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI.begin();
Serial.begin(57600);
}
void loop() {
if (Serial.available()) {
uint8_t c = Serial.read();
digitalWrite(10, LOW);
if (c == '1') {
SPI.transfer(0x01);
} else {
SPI.transfer(0x00);
}
digitalWrite(10, HIGH);
}
}
Slave:
#include "SPI.h"
#define LED 13
void setup() {
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
ISR (SPI_STC_vect) {
uint8_t c = SPDR;
uint8_t v = LOW;
if (c == 0x01) v = HIGH;
digitalWrite(LED, v);
}
void loop() {
}
@pylon
I have tested your codes having two ArduinoUNO in SPI Protocols.
I have connected an external LED at DPin-2 instead og DPin-13 as it is used for SCK of SPI.
The LED just flashes once in the ISR; it should be latched.
I could not find the reason of the this behaviour. Instead, I have executed the following codes
in the Slave. Now, the LED remains ON.
#include <SPI.h>
void setup()
{
pinMode(2, OUTPUT);
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
void loop()
{
}
ISR (SPI_STC_vect)
{
unsigned char c = SPDR;
if (c == 0x01)
digitalWrite(2, c);
}
I would appreciate your feedback. Many thanks for your foundation codes.
pylon
April 2, 2017, 5:50pm
4
I have connected an external LED at DPin-2 instead og DPin-13 as it is used for SCK of SPI.
Absolutely correct, my fault, please excuse.
The LED just flashes once in the ISR; it should be latched.
Probably because you're sending other bytes on the serial interface. If you send an end-of-line combo (CR/LF) that's twice a turn off command.
I could not find the reason of the this behaviour. Instead, I have executed the following codes
in the Slave. Now, the LED remains ON.
Yes, and you can never turn it off again.
Thanks for the prompt reply with very constructive comments.