Hello, and thanks in advance for your help... i'm having a problem with this setup.
I want to control a stepper attached to the master, with the parameters of speed and steps numbers given by the slave.
so, in theory, every time the master completes a cycle, it demands to the slave the speed and steps to do the next cycle.
BUT, i didnt finish the code because i'cant get the values from the slave.
when i request the only think i get is "-1"
i have created an array (called receipt) to hold the data, but testing out the master dont even get in the loop of
"while (Wire.available()) {...}"
so i write the folowing sentences just to force to read something from the uno... and when i print that on screen it shows -1...
as you can see on the slave code, it doenst matter how i command the "Wire.write()" neither sending a int (32) nor a byte (z)
please, i need some guidance here... im kinda beginner
Also i got them connected like:
UNO TX (pd1)-> NANO RX (pd0)
UNO RX (pd0)-> NANO TX (pd1)
UNO GND-> NANO GND
MASTER (NANO)
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <AccelStepper.h>
#include <Wire.h>
#define dirPin 6
#define stepPin 5
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
// SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
// MOSI is LCD DIN - this is pin 11 on an Arduino Uno
// pin 9 - Data/Command select (D/C)
// pin 8 - LCD chip select (CS)
// pin 7 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7);
const int endstopPin = 3;
const int sleep = 4;
int cadencia = 10;
int volume = 500;
float velocidad;
float nPasos;
int i;
int receipt[] = {1,2,3};
void setup() {
Serial.begin(9600);
Wire.begin();
display.begin();
display.setContrast(50);
display.setRotation(2);
display.clearDisplay();
escribir (0,0,"VOLUMEN",1);
escribir (0,10,"CADENCIA",1);
pinMode(endstopPin, INPUT);
pinMode(sleep, OUTPUT);
stepper.setAcceleration(1000.0);
stepper.setMaxSpeed(200);
stepper.setSpeed(-200);
i=0;
display.fillRect( 20, 40, 30, 10, WHITE);
while (i<3) {
escribir (20+10*i,40,String(receipt[i]),1);
i++;
}
//homming
activate();
while(digitalRead(endstopPin)!=HIGH){
stepper.runSpeed();
}
stepper.setCurrentPosition(0);
deactivate();
}
void loop() {
//escribe datos
display.fillRect( 60, 0, 30, 10, WHITE);
display.display();
escribir (60,0,String(volume),1);
escribir (60,10,String(cadencia),1);
delay (20);
//demanda parametros al esclavo
display.fillRect( 20, 40, 30, 10, WHITE);
i=0;
Wire.requestFrom(5,3);
while (Wire.available()) {
receipt [i]= Wire.read();
escribir (20+10*i,40,String(receipt[i]),1);
i++;
}
i=0;
while (i<3) {
receipt [i]= Wire.read();
escribir (20+10*i,40,String(receipt[i]),1);
i++;
}
//calcula parametros
int nPasos = volume*0.8076; // 200* VOL*10*pi()/i*pRosca*diam^2
int velocidad = cadencia*nPasos/100; // 2*CAD*(VOL*10*pi()/i*pRosca*diam^2)
display.fillRect( 0, 20, 60, 10, WHITE);
escribir (0,20,"np "+String(nPasos)+" vel "+String(velocidad),1);
// espera señal de arranque
//ciclado
stepper.setMaxSpeed(velocidad);
activate();
stepper.runToNewPosition(nPasos);
deactivate();
delay(1000);
activate();
stepper.runToNewPosition(0);
deactivate();
delay(1000);
}
//funciones utilizadas
void escribir(int16_t x, int16_t y, String texto, uint8_t s) {
display.setCursor(x, y);
display.setTextSize(s);
display.setTextColor(BLACK);
display.setTextWrap(false);
display.print(texto);
display.display();
}
void activate() {
digitalWrite(sleep,HIGH);
}
void deactivate() {
digitalWrite(sleep,LOW);
}
SLAVE (UNO)
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> // COLORES => ST7735_* (WHITE, BLACK, ETC)
#include <Arduino.h>
#include <Wire.h>
// SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
// MOSI is LCD DIN - this is pin 11 on an Arduino Uno
#define TFT_CS 2
#define TFT_RST 3
#define TFT_DC 4
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
const int minusPin = 5;
const int plusPin = 6;
const int setPin = 7;
int cadencia;
int volume;
bool minus0;
bool minus1;
bool plus0;
bool plus1;
bool set0;
bool set1;
byte SlaveReceived = 0;
void setup() {
Serial.begin(9600);
Wire.begin(5);
// Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.fillScreen(ST7735_BLACK);
pinMode(plusPin, INPUT);
pinMode(minusPin, INPUT);
pinMode(setPin, INPUT);
cadencia = 15;
volume = 600;
escribir (0,0,"VOLUMEN",2, ST7735_YELLOW);
escribir (0,50,"CADENCIA",2, ST7735_YELLOW);
escribir (35,20,String(volume),2, ST7735_WHITE);
escribir (35,70,String(cadencia),2, ST7735_WHITE);
}
void loop() {
delay(100);
//deteccion de entrada a configuracion
if(digitalRead(setPin)==HIGH){
delay(50);
while(digitalRead(setPin)!=LOW){}
//deteccion de botones PARA VOLUMEN
volume = buttons (volume, 35, 20, 1000, 500, 100);
//deteccion de botones PARA CADENCIA
cadencia = buttons (cadencia, 35, 70, 20, 12, 1);
}
}
//funciones utilizadas
void escribir(int16_t x, int16_t y, String texto, uint8_t s, uint16_t color) {
tft.setCursor(x, y);
tft.setTextSize(s);
tft.setTextColor(color);
tft.setTextWrap(false);
tft.print(texto);
}
int buttons (int variable, int X, int Y, int max, int min, int delta){
plus0=0;
minus0=0;
tft.fillRect( X, Y, 60, 20, ST7735_BLACK);
escribir (X,Y,String(variable),2, ST7735_RED);
while(digitalRead(setPin)!=HIGH){
plus1 = plus0;
plus0 = digitalRead(plusPin);
delay(50);
if (plus0 != plus1) {
if (plus0 == HIGH) { //se registra el cambio del boton delta, incrementando la cadencia y mostrandola en pantalla
tft.fillRect( X, Y, 60, 20, ST7735_BLACK);
variable= variable + delta;
if(variable > max) variable = max;
escribir (X,Y,String(variable),2, ST7735_RED);
}
}
minus1 = minus0;
minus0 = digitalRead(minusPin);
delay(50);
if (minus0 != minus1) {
if (minus0 == HIGH) { //se registra el cambio del boton delta, incrementando la cadencia y mostrandola en pantalla
tft.fillRect( X, Y, 60, 20, ST7735_BLACK);
variable= variable - delta;
if(variable < min) variable = min;
escribir (X,Y,String(variable),2, ST7735_RED);
}
}
}
tft.fillRect( X, Y, 60, 20, ST7735_BLACK);
escribir (X,Y,String(variable),2, ST7735_WHITE);
delay(50);
while(digitalRead(setPin)!=LOW){}
return (variable);
}
void requestEvent(){
// byte z = 35;
// Wire.write(z);
Wire.write(32);
}