03-03-2022, 01:37 PM
(Questo messaggio è stato modificato l'ultima volta il: 03-03-2022, 01:38 PM da lelef.)
intanto vi metto il codice
parte motion arduino nano
+++++++++++++++++++++++++++++++++++++++++++
// boring Head Receiver $ motion By LELEF
#include <SPI.h>
#include <RF24.h> // radio library;
#include <AccelStepper.h> // Include the AccelStepper library:
//impostazioni radio
#define pinCE 7 // On associe la broche "CE" du NRF24L01 à la sortie digitale D7 de l'arduino
#define pinCSN 8 // On associe la broche "CSN" du NRF24L01 à la sortie digitale D8 de l'arduino
#define tunnel1 "PIPE2" // On définit un premier "nom de tunnel" (5 caractères), pour pouvoir envoyer des données à l'autre NRF24
#define tunnel2 "PIPE1" // On définit un second "nom de tunnel" (5 caractères), pour pouvoir recevoir des données de l'autre NRF24
/* ----- NOTEZ L'INVERSION de PIPE1 et PIPE2, entre celui-ci et l'autre montage ! (car chaque tunnel véhicule les infos que dans un seul sens) */
RF24 radio(pinCE, pinCSN); // Instanciation du NRF24L01
const byte adresses[][8] = {tunnel1, tunnel2}; // Tableau des adresses de tunnel
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
unsigned int cicli = 1;
int passi = 0;
int velocita = 200;
byte acc = 30 ;
byte Azzera =0;
//*************************************************************//
// Ensemble des données à envoyer, par ondes radio (structure) //
//*************************************************************//
struct DatiRadio {
unsigned int cicliRADIO;
int passiRADIO;
int velocitaRADIO;
byte accRADIO;
byte AzzeraRadio;
};
DatiRadio DatiRadioBUFF;
void setup() {
//Serial.begin(9600); //debug
radio.begin(); // Initialisation du module NRF24
radio.openWritingPipe(adresses[0]); // Ouverture du "tunnel1" en ÉCRITURE
radio.openReadingPipe(1, adresses[1]); // Ouverture du "tunnel2" en LECTURE
radio.setPALevel(RF24_PA_LOW); // Sélection d'un niveau
radio.setDataRate(RF24_250KBPS); // Vitesse : RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
// Set the maximum speed and acceleration:
stepper.setCurrentPosition(0); //Resets the current position of the motor Has the side effect of setting the current motor speed to 0.
//Serial.println("carico radio");
}
void loop() {
// ******** RÉCEPTION ********
radio.startListening(); // On commence par arrêter le mode envoi, pour pouvoir réceptionner des données
if(radio.available()) { // On regarde si une donnée a été reçue
while (radio.available()) { // Si une donné est en attente de lecture, on va la lire
radio.read(&DatiRadioBUFF, sizeof(DatiRadioBUFF));// radio.read(&cicli, sizeof(cicli)); // Lecture des données reçues, une par une
cicli = DatiRadioBUFF.cicliRADIO;
passi = DatiRadioBUFF.passiRADIO;
velocita = DatiRadioBUFF.velocitaRADIO;
acc = DatiRadioBUFF.accRADIO;
Azzera = DatiRadioBUFF.AzzeraRadio;
stepper.setMaxSpeed(velocita);
stepper.setAcceleration(acc);
// Serial.println("cicli");
//Serial.println(cicli);
//Serial.println("passi");
//Serial.println(passi);
//Serial.println("velocita");
//Serial.println(velocita);
//Serial.println("acc");
//Serial.println(acc);
// Serial.println("Azzera");
//Serial.println(Azzera);
}
delay(20); // avec une petite pause, avant de reboucler
}
delay(5);
if (Azzera==1){
stepper.setCurrentPosition(0); //Resets the current position of the motor Has the side effect of setting the current motor speed to 0.
stepper.setMaxSpeed(velocita);
stepper.setAcceleration(acc);
Azzera=0;
cicli=1;
passi=0;
// Serial.println("azzerato");
}
else
{
if (cicli == 0 )
{
// Set the target position:
stepper.setMaxSpeed(velocita);
stepper.setAcceleration(acc);
stepper.move(passi);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
cicli = 1; // per sicurezza azzero cicli
// Serial.println("posizionato");
passi=0;
delay(3000);
trasmetti();
}
}
if (cicli > 1 ) {
// Set the target position:
stepper.moveTo(passi);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
// Move back to zero:
stepper.moveTo(0);
stepper.runToPosition();
cicli--;
//ddddddddSerial.println(cicli) ;
//if (cicli == 1 ) {
trasmetti() ;
//}
}
}
void trasmetti()
{
// ******** ENVOI ********
radio.stopListening(); // On commence par arrêter le mode écoute, pour pouvoir émettre les données
DatiRadioBUFF.cicliRADIO = cicli;
DatiRadioBUFF.passiRADIO = stepper.currentPosition ( ) ; // leggo posizione reale stepper e invio
DatiRadioBUFF.velocitaRADIO = velocita ;
DatiRadioBUFF.accRADIO = acc ;
DatiRadioBUFF.AzzeraRadio= Azzera;
radio.write(&DatiRadioBUFF, sizeof(DatiRadioBUFF)); // … et on envoi cette valeur à l'autre arduino, via le NRF24
delay(5);
return;
}
parte comando arduino uno
++++++++++++++++++++++++++++++++++++++++++++++++++
// boring Head Trasmitter module By LELEF
#include <SPI.h>
#include <RF24.h> // radio library;
//#include <Arduino.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
//impostazioni radio
#define pinCE 2 // On associe la broche "CE" du NRF24L01 à la sortie digitale D7 de l'arduino
#define pinCSN 3 // On associe la broche "CSN" du NRF24L01 à la sortie digitale D8 de l'arduino
#define tunnel1 "PIPE1" // On définit un premier "nom de tunnel" (5 caractères), pour pouvoir envoyer des données à l'autre NRF24
#define tunnel2 "PIPE2" // On définit un second "nom de tunnel" (5 caractères), pour pouvoir recevoir des données de l'autre NRF24
/* ----- NOTEZ L'INVERSION de PIPE1 et PIPE2, entre celui-ci et l'autre montage ! (car chaque tunnel véhicule les infos que dans un seul sens) */
#define CW HIGH // Define direction of rotation -
#define CCW LOW // If rotation needs to be reversed, swap HIGH and LOW here
// define menu screen ids
#define mainmenu 0
#define setzero 1
#define jogmode 2
#define runmode 3
#define setmode 4
//#define slavemode 5
#define numModes 4 // number of above menu items to choose, not counting main menu
#define distance 12
#define distanceCent 14
#define quanticicli 16
#define setmodespeed 18
#define AnalogKeyPin A0 // keypad uses A0
#define ADSettleTime 10 // ms delay to let AD converter "settle" i.e., discharge
// create lcd display construct
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Data Structure and Pin Numbers for the LCD/Keypad
// define LCD/Keypad buttons
#define NO_KEY 0
#define SELECT_KEY 1
#define LEFT_KEY 2
#define UP_KEY 3
#define DOWN_KEY 4
#define RIGHT_KEY 5
// create global variables
int cur_mode = mainmenu;
int mode_select = setzero;
int current_menu;
int cur_key;
int cur_pos = 0;
int cur_dir = CW;
int inutile = 0; //dati da scartare
int inutileDUE = -2; //dati da scartare
RF24 radio(pinCE, pinCSN); // Instanciation du NRF24L01
const byte adresses[][8] = {tunnel1, tunnel2}; // Tableau des adresses de tunnel
unsigned int cicli = 1;
int passi = 100;
int passiNegativi = -1;
int velocita = 200;
byte acc = 30 ;
byte Azzera =0;
float virgola=0;
int CHECKvirgola=0;
//*************************************************************//
// Ensemble des données à envoyer, par ondes radio (structure) //
//*************************************************************//
struct DatiRadio {
unsigned int cicliRADIO;
int passiRADIO;
int velocitaRADIO;
byte accRADIO;
byte AzzeraRadio;
};
DatiRadio DatiRadioBUFF;
void setup() {
// begin program
//************************** leggo eprom
acc = EEPROM.read(0);
velocita = (EEPROM.read(2) << 8);
velocita |= EEPROM.read(1);
/////////////////////////
//Serial.begin(9600);
virgola=passi ;
virgola=virgola/100;
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0); // display flash screen
lcd.print("Boring Head V.b2");
lcd.setCursor(0,1); // display flash screen
lcd.print(" Diameter Mode");
delay(1600); // wait a few secs
lcd.clear();
displayscreen(cur_mode); // put up initial menu screen
radio.begin(); // Initialisation du module NRF24
radio.openWritingPipe(adresses[0]); // Ouverture du "tunnel1" en ÉCRITURE
radio.openReadingPipe(1, adresses[1]); // Ouverture du "tunnel2" en LECTURE
radio.setPALevel(RF24_PA_LOW); // Sélection d'un niveau
radio.setDataRate(RF24_250KBPS); // Vitesse : RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
radio.stopListening(); // per sicurezza
//delay(20);
}
void loop() {
int exitflag;
displayscreen(cur_mode); // display the screen
cur_key = get_real_key(); // grab a keypress
switch(cur_mode) // execute the keystroke
{
case mainmenu: // main menu
switch(cur_key)
{
case UP_KEY:
mode_select++;
if (mode_select > numModes) // wrap around menu
mode_select = 1;
break;
case DOWN_KEY:
mode_select--;
if (mode_select < 1) // wrap around menu
mode_select = numModes;
break;
case LEFT_KEY: // left and right keys do nothing in main menu
break;
case RIGHT_KEY:
break;
case SELECT_KEY: // user has picked a menu item
cur_mode = mode_select;
break;
}
break;
case setzero: // call zero
dosetzero(cur_key);
cur_mode = mainmenu;
break;
case runmode: // call run
dorunmode(cur_key);
cur_mode = mainmenu;
break;
case jogmode: // call jog
dojogmode(cur_key);
cur_mode = mainmenu;
break;
case setmode: // call jog
dosetmode(cur_key);
cur_mode = mainmenu;
break;
}
delay(5);
}
void dosetmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(setmode); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
if (acc < 250) {
acc++;
displayscreen(setmode);
}
break;
case DOWN_KEY: // bump down the speed
if (acc > 1) {
acc--;
displayscreen(setmode);
}
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dosetmodespeed( tmp_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dosetmodespeed(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(setmodespeed); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
if (velocita < 499) {
velocita++;
displayscreen(setmodespeed);
}
break;
case DOWN_KEY: // bump down the speed
if (velocita > 10) {
velocita--;
displayscreen(setmodespeed);
}
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
EEPROM.update(0, acc);
EEPROM.update(1, velocita);
EEPROM.update(2, velocita >> 8);
lcd.setCursor(0,0);
lcd.print(" > Data Saved <");
lcd.setCursor(0,1);
lcd.print(" *****DONE*****");
delay(500);
cur_mode = mainmenu; // go back to main menu
breakflag = 1;
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dosetzero(int tmp_key)
{ int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW;
// displayscreen(quanticicli); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
// displayscreen(quanticicli);
//
break;
case DOWN_KEY: // bump down the speed
// displayscreen(quanticicli);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
Azzera=1;
trasmetti();
lcd.setCursor(0,1);
CHECKvirgola=0;
lcd.print(" *****DONE*****");
delay(500);
cur_mode = mainmenu; // go back to main menu
delay(5);
breakflag = 1; // fall through to exit
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dociclenmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(quanticicli); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
cicli++;
displayscreen(quanticicli);
break;
case DOWN_KEY: // bump down the speed
if (cicli>3)
{ cicli--;
}
displayscreen(quanticicli);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
Azzera=0; // x sicurezza
trasmetti();
delay(5);
inutileDUE =cicli;
lcd.clear();
while (inutileDUE!=1)
{
ricevi();
lcd.setCursor(0,0);
lcd.print("PASSES to DO ");
lcd.setCursor(14,0);
lcd.print(inutileDUE-1);
lcd.setCursor(0,1);
lcd.print(" - -");
delay(200);
lcd.setCursor(0,1);
lcd.print(" \ /");
delay(200);
lcd.setCursor(0,1);
lcd.print(" | |");
delay(200);
lcd.setCursor(0,1);
lcd.print(" / \ "); //senza spazio il compilatore interpreta ommento!!
delay(200);
}
if (inutileDUE==1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-****-DONE-****-");
lcd.setCursor(0,1);
lcd.print("> KEY to repeat");
// Serial.println("fatto");
delay(2000);
}
breakflag = 1; // fall through to exit
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dodistanceCENTnmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(distanceCent); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
passi++;
displayscreen(distanceCent);
break;
case DOWN_KEY: // bump down the speed
passi--;
displayscreen(distanceCent);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dociclenmode(cur_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dodistancenmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(distance); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
passi=passi+100;
displayscreen(distance);
break;
case DOWN_KEY: // bump down the speed
passi=passi-100;
displayscreen(distance);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dodistanceCENTnmode(cur_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dorunmode(int tmp_key)
{
cicli = 2;
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(runmode); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
if (velocita < 490) {
velocita=velocita+10;
displayscreen(runmode);
}
break;
case DOWN_KEY: // bump down the speed
if (velocita > 10) {
velocita = velocita-10;
displayscreen(runmode);
}
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dodistancenmode(cur_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dojogmode(int tmp_key)
{
int breakflag = 0;
for (;breakflag==0
{
switch(tmp_key)
{
case UP_KEY: // bump the number of steps
if (passi>=30000)
{
break;
}
if (passi>=1000)
{
passi=passi+1000;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>=100)
{
passi=passi+100;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>=10)
{
passi=passi+10;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi >= 1)
{
passi++;
virgola=passi ;
virgola=virgola/100;
break;
}
case DOWN_KEY: // reduce the number of steps
if (passi>1000)
{
passi=passi-1000;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>100)
{
passi=passi-100;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>10)
{
passi=passi-10;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi > 1)
{
passi--;
virgola=passi ;
virgola=virgola/100;
break;
}
case LEFT_KEY: // step the motor CCW
Azzera=0;
cicli = 0;
CHECKvirgola =CHECKvirgola-passi;
passi=passi*passiNegativi; //backwards
trasmetti() ;
passi=passi*passiNegativi; // reset to+
cicli = 2;
lcd.clear();
ricevi();
while ( CHECKvirgola!=inutile) {
//leggi sino a svuotare buffer
for (int i = 16; i >= 0; i--) {
lcd.setCursor(i,0);
lcd.print("<");
delay(100);
}
for (int i = 16; i >= 0; i--)
{
ricevi();
lcd.setCursor(i,0);
lcd.print(" ");
delay(100);
}
ricevi();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-****-DONE-****-");
lcd.setCursor(0,1);
lcd.print(" Pos MM ");
lcd.setCursor(9,1);
virgola=inutile ;
virgola=virgola/100;
lcd.print(virgola,2);
// Serial.println("passi");
// Serial.println(inutile);
// Serial.println("fatto");
delay(2000);
break;
case RIGHT_KEY: // step the motor CW
CHECKvirgola =CHECKvirgola+passi;
Azzera=0;
cicli = 0;
trasmetti();
cicli = 2;
lcd.clear();
ricevi();
while ( CHECKvirgola!=inutile) {
for (int i = 0; i <= 16; i++)
{
//leggi sino a svuotare buffe
lcd.setCursor(i,0);
lcd.print(">");
delay(100);
}
for (int i = 0; i <= 16; i++)
{
//leggi sino a svuotare buffe
lcd.setCursor(i,0);
lcd.print(" ");
delay(100);
}
ricevi();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-****-DONE-****-");
lcd.setCursor(0,1);
lcd.print(" Pos MM ");
lcd.setCursor(9,1);
virgola=inutile ;
virgola=virgola/100;
lcd.print(virgola,2);
// Serial.println("passi");
// Serial.println(inutile);
// Serial.println("fatto");
delay(2000);
break;
case SELECT_KEY: // user want to quit
mode_select = setzero;
breakflag = 1;
break;
}
if (breakflag == 0)
{
displayscreen(jogmode);
tmp_key = get_real_key();
}
}
cur_mode = mainmenu; // go back to main menu
return;
}
void displayscreen(int menunum) // screen displays are here
{
lcd.clear();
lcd.setCursor(0,0);
switch (menunum)
{
case mainmenu:
lcd.print("Select Mode");
lcd.setCursor(0,1);
lcd.print("Mode = ");
lcd.setCursor(7,1);
switch(mode_select)
{
case(setzero):
lcd.print("Set Zero");
break;
case(runmode):
lcd.print("Run");
break;
case(jogmode):
lcd.print("Jog");
break;
case(setmode):
lcd.print("Settings");
break;
}
break;
case setmode:
lcd.setCursor(0,0);
lcd.print("Feedrate > ");
lcd.setCursor(0,1);
lcd.print("Accel ");
lcd.setCursor(10,1);
lcd.print((int)acc);
break;
case setmodespeed:
lcd.setCursor(0,0);
lcd.print("Save Data > ");
lcd.setCursor(0,1);
lcd.print("Feedrate ");
lcd.setCursor(10,1);
lcd.print((int)velocita);
break;
case setzero:
lcd.setCursor(0,0);
lcd.print("Set Origin 0.00") ;
lcd.setCursor(0,1);
lcd.print(" ***Execute?***");
break;
case runmode:
lcd.setCursor(0,0);
lcd.print("MM to go > ");
lcd.setCursor(0,1);
lcd.print("Feedrate ");
lcd.setCursor(10,1);
lcd.print((int)velocita);
break;
case distance:
lcd.setCursor(0,0);
lcd.print(".00MM to go >");
lcd.setCursor(0,1);
lcd.print("Go to MM ");
lcd.setCursor(10,1);
virgola=passi ;
virgola=virgola/100;
lcd.print(virgola,2);
break;
case distanceCent:
lcd.setCursor(0,0);
lcd.print("Cicles to do > ");
lcd.setCursor(0,1);
lcd.print("Go to MM ");
lcd.setCursor(10,1);
virgola=passi ;
virgola=virgola/100;
lcd.print(virgola,2);
break;
case quanticicli:
lcd.setCursor(0,0);
lcd.print("Send & Run > ");
lcd.setCursor(0,1);
lcd.print("N of Cicles");
lcd.setCursor(12,1);
lcd.print((int)cicli-1);
break;
case jogmode:
virgola=passi ;
virgola=virgola/100;
if (virgola>=300)
{
lcd.print("Max Step Reached") ;
}
else
{
lcd.print("Jog < >");
}
lcd.setCursor(0,1);
lcd.print("MM - step");
lcd.setCursor(11,1);
lcd.print(virgola,2);
break;
}
return;
}
void trasmetti()
{
// ******** ENVOI ********
radio.stopListening(); // On commence par arrêter le mode écoute, pour pouvoir émettre les données
DatiRadioBUFF.cicliRADIO = cicli;
DatiRadioBUFF.passiRADIO = passi;
DatiRadioBUFF.velocitaRADIO = velocita ;
DatiRadioBUFF.accRADIO = acc ;
DatiRadioBUFF.AzzeraRadio= Azzera;
radio.write(&DatiRadioBUFF, sizeof(DatiRadioBUFF)); // … et on envoi cette valeur à l'autre arduino, via le NRF24
// Serial.println("cicli");
//Serial.println(cicli);
// Serial.println("passi");
// Serial.println(passi);
// Serial.println("velocita");
// Serial.println(velocita);
// Serial.println("acc");
// Serial.println(acc);
// Serial.println("Azzera");
// Serial.println(Azzera);
//delay(5);
return;
}
void ricevi()
{
// ******** RÉCEPTION ********
radio.openWritingPipe(adresses[0]); // Ouverture du "tunnel1" en ÉCRITURE
radio.openReadingPipe(1, adresses[1]); // Ouverture du "tunnel2" en LECTURE
radio.startListening();
// On commence par arrêter le mode envoi, pour pouvoir réceptionner des données
if(radio.available()) { // On regarde si une donnée a été reçue
while (radio.available()) { // Si une donné est en attente de lecture, on va la lire
radio.read(&DatiRadioBUFF, sizeof(DatiRadioBUFF));// radio.read(&cicli, sizeof(cicli)); // Lecture des données reçues, une par une
inutileDUE = DatiRadioBUFF.cicliRADIO;
// inutile = DatiRadioBUFF.velocitaRADIO;
//inutile = DatiRadioBUFF.accRADIO;
//inutile = DatiRadioBUFF.AzzeraRadio;
inutile = DatiRadioBUFF.passiRADIO;
}
delay(20); // avec une petite pause, avant de reboucler
}
delay(5);
return;
}
int get_real_key(void) // routine to return a valid keystroke
{
int trial_key = 0;
while (trial_key < 1)
{
trial_key = read_LCD_button();
}
delay(200); // 200 millisec delay between user keys
return trial_key;
}
int read_LCD_button() // routine to read the LCD's buttons
{
int key_in;
delay(ADSettleTime); // wait to settle
key_in = analogRead(0); // read ADC once
delay(ADSettleTime); // wait to settle
// average values for my board were: 0, 144, 324, 505, 742
// add approx 100 to those values to set range
if (key_in > 800) return NO_KEY;
if (key_in < 60) return RIGHT_KEY;
if (key_in < 200) return UP_KEY;
if (key_in < 400) return DOWN_KEY;
if (key_in < 600) return LEFT_KEY;
if (key_in < 800) return SELECT_KEY;
}
void lineariInterpolation()
{
Serial.begin(57600);
// if(Serial.available() && sw)
// {
// for(int j = 0; j < 3; j++)
// {
// input[j] = Serial.read();
// }
//command = ((String)input).toInt();
// sw = 0;
int Lstart;
int Lend;
int Lspeed;
int Lradius; // to tell the module it isn t a sfere
Serial.end();
}
void SfericalInterpolation()
{
Serial.begin(57600);
// if(Serial.available() && sw)
// {
// for(int j = 0; j < 3; j++)
// {
// input[j] = Serial.read();
// }
//command = ((String)input).toInt();
// sw = 0;
//}
// da mettere sul ricevitore
int Sstart;
int Send;
int Sspeed;
int Sradius;
int risol;
int fattore;
int gradi;
int X;
int radius;
fattore=360/ risol; //risoluzione coordinate
gradi=fattore*3.14/180;//convert to degrees per positon radians
for (int k = 0; k <=fattore-1; k++) {
gradi=gradi*k;
X=radius*cos(gradi);
//y=radius*sin(gradi);
}
// END da mettere sul ricevitore
Serial.end();
}
parte motion arduino nano
+++++++++++++++++++++++++++++++++++++++++++
// boring Head Receiver $ motion By LELEF
#include <SPI.h>
#include <RF24.h> // radio library;
#include <AccelStepper.h> // Include the AccelStepper library:
//impostazioni radio
#define pinCE 7 // On associe la broche "CE" du NRF24L01 à la sortie digitale D7 de l'arduino
#define pinCSN 8 // On associe la broche "CSN" du NRF24L01 à la sortie digitale D8 de l'arduino
#define tunnel1 "PIPE2" // On définit un premier "nom de tunnel" (5 caractères), pour pouvoir envoyer des données à l'autre NRF24
#define tunnel2 "PIPE1" // On définit un second "nom de tunnel" (5 caractères), pour pouvoir recevoir des données de l'autre NRF24
/* ----- NOTEZ L'INVERSION de PIPE1 et PIPE2, entre celui-ci et l'autre montage ! (car chaque tunnel véhicule les infos que dans un seul sens) */
RF24 radio(pinCE, pinCSN); // Instanciation du NRF24L01
const byte adresses[][8] = {tunnel1, tunnel2}; // Tableau des adresses de tunnel
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
unsigned int cicli = 1;
int passi = 0;
int velocita = 200;
byte acc = 30 ;
byte Azzera =0;
//*************************************************************//
// Ensemble des données à envoyer, par ondes radio (structure) //
//*************************************************************//
struct DatiRadio {
unsigned int cicliRADIO;
int passiRADIO;
int velocitaRADIO;
byte accRADIO;
byte AzzeraRadio;
};
DatiRadio DatiRadioBUFF;
void setup() {
//Serial.begin(9600); //debug
radio.begin(); // Initialisation du module NRF24
radio.openWritingPipe(adresses[0]); // Ouverture du "tunnel1" en ÉCRITURE
radio.openReadingPipe(1, adresses[1]); // Ouverture du "tunnel2" en LECTURE
radio.setPALevel(RF24_PA_LOW); // Sélection d'un niveau
radio.setDataRate(RF24_250KBPS); // Vitesse : RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
// Set the maximum speed and acceleration:
stepper.setCurrentPosition(0); //Resets the current position of the motor Has the side effect of setting the current motor speed to 0.
//Serial.println("carico radio");
}
void loop() {
// ******** RÉCEPTION ********
radio.startListening(); // On commence par arrêter le mode envoi, pour pouvoir réceptionner des données
if(radio.available()) { // On regarde si une donnée a été reçue
while (radio.available()) { // Si une donné est en attente de lecture, on va la lire
radio.read(&DatiRadioBUFF, sizeof(DatiRadioBUFF));// radio.read(&cicli, sizeof(cicli)); // Lecture des données reçues, une par une
cicli = DatiRadioBUFF.cicliRADIO;
passi = DatiRadioBUFF.passiRADIO;
velocita = DatiRadioBUFF.velocitaRADIO;
acc = DatiRadioBUFF.accRADIO;
Azzera = DatiRadioBUFF.AzzeraRadio;
stepper.setMaxSpeed(velocita);
stepper.setAcceleration(acc);
// Serial.println("cicli");
//Serial.println(cicli);
//Serial.println("passi");
//Serial.println(passi);
//Serial.println("velocita");
//Serial.println(velocita);
//Serial.println("acc");
//Serial.println(acc);
// Serial.println("Azzera");
//Serial.println(Azzera);
}
delay(20); // avec une petite pause, avant de reboucler
}
delay(5);
if (Azzera==1){
stepper.setCurrentPosition(0); //Resets the current position of the motor Has the side effect of setting the current motor speed to 0.
stepper.setMaxSpeed(velocita);
stepper.setAcceleration(acc);
Azzera=0;
cicli=1;
passi=0;
// Serial.println("azzerato");
}
else
{
if (cicli == 0 )
{
// Set the target position:
stepper.setMaxSpeed(velocita);
stepper.setAcceleration(acc);
stepper.move(passi);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
cicli = 1; // per sicurezza azzero cicli
// Serial.println("posizionato");
passi=0;
delay(3000);
trasmetti();
}
}
if (cicli > 1 ) {
// Set the target position:
stepper.moveTo(passi);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
// Move back to zero:
stepper.moveTo(0);
stepper.runToPosition();
cicli--;
//ddddddddSerial.println(cicli) ;
//if (cicli == 1 ) {
trasmetti() ;
//}
}
}
void trasmetti()
{
// ******** ENVOI ********
radio.stopListening(); // On commence par arrêter le mode écoute, pour pouvoir émettre les données
DatiRadioBUFF.cicliRADIO = cicli;
DatiRadioBUFF.passiRADIO = stepper.currentPosition ( ) ; // leggo posizione reale stepper e invio
DatiRadioBUFF.velocitaRADIO = velocita ;
DatiRadioBUFF.accRADIO = acc ;
DatiRadioBUFF.AzzeraRadio= Azzera;
radio.write(&DatiRadioBUFF, sizeof(DatiRadioBUFF)); // … et on envoi cette valeur à l'autre arduino, via le NRF24
delay(5);
return;
}
parte comando arduino uno
++++++++++++++++++++++++++++++++++++++++++++++++++
// boring Head Trasmitter module By LELEF
#include <SPI.h>
#include <RF24.h> // radio library;
//#include <Arduino.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
//impostazioni radio
#define pinCE 2 // On associe la broche "CE" du NRF24L01 à la sortie digitale D7 de l'arduino
#define pinCSN 3 // On associe la broche "CSN" du NRF24L01 à la sortie digitale D8 de l'arduino
#define tunnel1 "PIPE1" // On définit un premier "nom de tunnel" (5 caractères), pour pouvoir envoyer des données à l'autre NRF24
#define tunnel2 "PIPE2" // On définit un second "nom de tunnel" (5 caractères), pour pouvoir recevoir des données de l'autre NRF24
/* ----- NOTEZ L'INVERSION de PIPE1 et PIPE2, entre celui-ci et l'autre montage ! (car chaque tunnel véhicule les infos que dans un seul sens) */
#define CW HIGH // Define direction of rotation -
#define CCW LOW // If rotation needs to be reversed, swap HIGH and LOW here
// define menu screen ids
#define mainmenu 0
#define setzero 1
#define jogmode 2
#define runmode 3
#define setmode 4
//#define slavemode 5
#define numModes 4 // number of above menu items to choose, not counting main menu
#define distance 12
#define distanceCent 14
#define quanticicli 16
#define setmodespeed 18
#define AnalogKeyPin A0 // keypad uses A0
#define ADSettleTime 10 // ms delay to let AD converter "settle" i.e., discharge
// create lcd display construct
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Data Structure and Pin Numbers for the LCD/Keypad
// define LCD/Keypad buttons
#define NO_KEY 0
#define SELECT_KEY 1
#define LEFT_KEY 2
#define UP_KEY 3
#define DOWN_KEY 4
#define RIGHT_KEY 5
// create global variables
int cur_mode = mainmenu;
int mode_select = setzero;
int current_menu;
int cur_key;
int cur_pos = 0;
int cur_dir = CW;
int inutile = 0; //dati da scartare
int inutileDUE = -2; //dati da scartare
RF24 radio(pinCE, pinCSN); // Instanciation du NRF24L01
const byte adresses[][8] = {tunnel1, tunnel2}; // Tableau des adresses de tunnel
unsigned int cicli = 1;
int passi = 100;
int passiNegativi = -1;
int velocita = 200;
byte acc = 30 ;
byte Azzera =0;
float virgola=0;
int CHECKvirgola=0;
//*************************************************************//
// Ensemble des données à envoyer, par ondes radio (structure) //
//*************************************************************//
struct DatiRadio {
unsigned int cicliRADIO;
int passiRADIO;
int velocitaRADIO;
byte accRADIO;
byte AzzeraRadio;
};
DatiRadio DatiRadioBUFF;
void setup() {
// begin program
//************************** leggo eprom
acc = EEPROM.read(0);
velocita = (EEPROM.read(2) << 8);
velocita |= EEPROM.read(1);
/////////////////////////
//Serial.begin(9600);
virgola=passi ;
virgola=virgola/100;
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0); // display flash screen
lcd.print("Boring Head V.b2");
lcd.setCursor(0,1); // display flash screen
lcd.print(" Diameter Mode");
delay(1600); // wait a few secs
lcd.clear();
displayscreen(cur_mode); // put up initial menu screen
radio.begin(); // Initialisation du module NRF24
radio.openWritingPipe(adresses[0]); // Ouverture du "tunnel1" en ÉCRITURE
radio.openReadingPipe(1, adresses[1]); // Ouverture du "tunnel2" en LECTURE
radio.setPALevel(RF24_PA_LOW); // Sélection d'un niveau
radio.setDataRate(RF24_250KBPS); // Vitesse : RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
radio.stopListening(); // per sicurezza
//delay(20);
}
void loop() {
int exitflag;
displayscreen(cur_mode); // display the screen
cur_key = get_real_key(); // grab a keypress
switch(cur_mode) // execute the keystroke
{
case mainmenu: // main menu
switch(cur_key)
{
case UP_KEY:
mode_select++;
if (mode_select > numModes) // wrap around menu
mode_select = 1;
break;
case DOWN_KEY:
mode_select--;
if (mode_select < 1) // wrap around menu
mode_select = numModes;
break;
case LEFT_KEY: // left and right keys do nothing in main menu
break;
case RIGHT_KEY:
break;
case SELECT_KEY: // user has picked a menu item
cur_mode = mode_select;
break;
}
break;
case setzero: // call zero
dosetzero(cur_key);
cur_mode = mainmenu;
break;
case runmode: // call run
dorunmode(cur_key);
cur_mode = mainmenu;
break;
case jogmode: // call jog
dojogmode(cur_key);
cur_mode = mainmenu;
break;
case setmode: // call jog
dosetmode(cur_key);
cur_mode = mainmenu;
break;
}
delay(5);
}
void dosetmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(setmode); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
if (acc < 250) {
acc++;
displayscreen(setmode);
}
break;
case DOWN_KEY: // bump down the speed
if (acc > 1) {
acc--;
displayscreen(setmode);
}
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dosetmodespeed( tmp_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dosetmodespeed(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(setmodespeed); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
if (velocita < 499) {
velocita++;
displayscreen(setmodespeed);
}
break;
case DOWN_KEY: // bump down the speed
if (velocita > 10) {
velocita--;
displayscreen(setmodespeed);
}
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
EEPROM.update(0, acc);
EEPROM.update(1, velocita);
EEPROM.update(2, velocita >> 8);
lcd.setCursor(0,0);
lcd.print(" > Data Saved <");
lcd.setCursor(0,1);
lcd.print(" *****DONE*****");
delay(500);
cur_mode = mainmenu; // go back to main menu
breakflag = 1;
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dosetzero(int tmp_key)
{ int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW;
// displayscreen(quanticicli); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
// displayscreen(quanticicli);
//
break;
case DOWN_KEY: // bump down the speed
// displayscreen(quanticicli);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
Azzera=1;
trasmetti();
lcd.setCursor(0,1);
CHECKvirgola=0;
lcd.print(" *****DONE*****");
delay(500);
cur_mode = mainmenu; // go back to main menu
delay(5);
breakflag = 1; // fall through to exit
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dociclenmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(quanticicli); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
cicli++;
displayscreen(quanticicli);
break;
case DOWN_KEY: // bump down the speed
if (cicli>3)
{ cicli--;
}
displayscreen(quanticicli);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
Azzera=0; // x sicurezza
trasmetti();
delay(5);
inutileDUE =cicli;
lcd.clear();
while (inutileDUE!=1)
{
ricevi();
lcd.setCursor(0,0);
lcd.print("PASSES to DO ");
lcd.setCursor(14,0);
lcd.print(inutileDUE-1);
lcd.setCursor(0,1);
lcd.print(" - -");
delay(200);
lcd.setCursor(0,1);
lcd.print(" \ /");
delay(200);
lcd.setCursor(0,1);
lcd.print(" | |");
delay(200);
lcd.setCursor(0,1);
lcd.print(" / \ "); //senza spazio il compilatore interpreta ommento!!
delay(200);
}
if (inutileDUE==1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-****-DONE-****-");
lcd.setCursor(0,1);
lcd.print("> KEY to repeat");
// Serial.println("fatto");
delay(2000);
}
breakflag = 1; // fall through to exit
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dodistanceCENTnmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(distanceCent); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
passi++;
displayscreen(distanceCent);
break;
case DOWN_KEY: // bump down the speed
passi--;
displayscreen(distanceCent);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dociclenmode(cur_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dodistancenmode(int tmp_key)
{
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(distance); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
passi=passi+100;
displayscreen(distance);
break;
case DOWN_KEY: // bump down the speed
passi=passi-100;
displayscreen(distance);
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dodistanceCENTnmode(cur_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dorunmode(int tmp_key)
{
cicli = 2;
int breakflag = 0;
delay(100); // wait for keybounce from user's selection
cur_dir = CW; // initially, clockwise
displayscreen(runmode); // show the screen
while (breakflag == 0) // cycle until Select Key sets flag
{
if (analogRead(AnalogKeyPin) < 850 ) // if a keypress is present {
{
cur_key = get_real_key(); // then get it
switch(cur_key) // and honor it
{
case UP_KEY: // bump up the speed
if (velocita < 490) {
velocita=velocita+10;
displayscreen(runmode);
}
break;
case DOWN_KEY: // bump down the speed
if (velocita > 10) {
velocita = velocita-10;
displayscreen(runmode);
}
break;
case LEFT_KEY:
break;
case RIGHT_KEY: // set other direction
dodistancenmode(cur_key);
break;
case SELECT_KEY: // user wants to stop
// passa al successivo
breakflag = 1; // fall through to exit
}
}
}
}
void dojogmode(int tmp_key)
{
int breakflag = 0;
for (;breakflag==0
{
switch(tmp_key)
{
case UP_KEY: // bump the number of steps
if (passi>=30000)
{
break;
}
if (passi>=1000)
{
passi=passi+1000;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>=100)
{
passi=passi+100;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>=10)
{
passi=passi+10;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi >= 1)
{
passi++;
virgola=passi ;
virgola=virgola/100;
break;
}
case DOWN_KEY: // reduce the number of steps
if (passi>1000)
{
passi=passi-1000;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>100)
{
passi=passi-100;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi>10)
{
passi=passi-10;
virgola=passi ;
virgola=virgola/100;
break;
}
if (passi > 1)
{
passi--;
virgola=passi ;
virgola=virgola/100;
break;
}
case LEFT_KEY: // step the motor CCW
Azzera=0;
cicli = 0;
CHECKvirgola =CHECKvirgola-passi;
passi=passi*passiNegativi; //backwards
trasmetti() ;
passi=passi*passiNegativi; // reset to+
cicli = 2;
lcd.clear();
ricevi();
while ( CHECKvirgola!=inutile) {
//leggi sino a svuotare buffer
for (int i = 16; i >= 0; i--) {
lcd.setCursor(i,0);
lcd.print("<");
delay(100);
}
for (int i = 16; i >= 0; i--)
{
ricevi();
lcd.setCursor(i,0);
lcd.print(" ");
delay(100);
}
ricevi();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-****-DONE-****-");
lcd.setCursor(0,1);
lcd.print(" Pos MM ");
lcd.setCursor(9,1);
virgola=inutile ;
virgola=virgola/100;
lcd.print(virgola,2);
// Serial.println("passi");
// Serial.println(inutile);
// Serial.println("fatto");
delay(2000);
break;
case RIGHT_KEY: // step the motor CW
CHECKvirgola =CHECKvirgola+passi;
Azzera=0;
cicli = 0;
trasmetti();
cicli = 2;
lcd.clear();
ricevi();
while ( CHECKvirgola!=inutile) {
for (int i = 0; i <= 16; i++)
{
//leggi sino a svuotare buffe
lcd.setCursor(i,0);
lcd.print(">");
delay(100);
}
for (int i = 0; i <= 16; i++)
{
//leggi sino a svuotare buffe
lcd.setCursor(i,0);
lcd.print(" ");
delay(100);
}
ricevi();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("-****-DONE-****-");
lcd.setCursor(0,1);
lcd.print(" Pos MM ");
lcd.setCursor(9,1);
virgola=inutile ;
virgola=virgola/100;
lcd.print(virgola,2);
// Serial.println("passi");
// Serial.println(inutile);
// Serial.println("fatto");
delay(2000);
break;
case SELECT_KEY: // user want to quit
mode_select = setzero;
breakflag = 1;
break;
}
if (breakflag == 0)
{
displayscreen(jogmode);
tmp_key = get_real_key();
}
}
cur_mode = mainmenu; // go back to main menu
return;
}
void displayscreen(int menunum) // screen displays are here
{
lcd.clear();
lcd.setCursor(0,0);
switch (menunum)
{
case mainmenu:
lcd.print("Select Mode");
lcd.setCursor(0,1);
lcd.print("Mode = ");
lcd.setCursor(7,1);
switch(mode_select)
{
case(setzero):
lcd.print("Set Zero");
break;
case(runmode):
lcd.print("Run");
break;
case(jogmode):
lcd.print("Jog");
break;
case(setmode):
lcd.print("Settings");
break;
}
break;
case setmode:
lcd.setCursor(0,0);
lcd.print("Feedrate > ");
lcd.setCursor(0,1);
lcd.print("Accel ");
lcd.setCursor(10,1);
lcd.print((int)acc);
break;
case setmodespeed:
lcd.setCursor(0,0);
lcd.print("Save Data > ");
lcd.setCursor(0,1);
lcd.print("Feedrate ");
lcd.setCursor(10,1);
lcd.print((int)velocita);
break;
case setzero:
lcd.setCursor(0,0);
lcd.print("Set Origin 0.00") ;
lcd.setCursor(0,1);
lcd.print(" ***Execute?***");
break;
case runmode:
lcd.setCursor(0,0);
lcd.print("MM to go > ");
lcd.setCursor(0,1);
lcd.print("Feedrate ");
lcd.setCursor(10,1);
lcd.print((int)velocita);
break;
case distance:
lcd.setCursor(0,0);
lcd.print(".00MM to go >");
lcd.setCursor(0,1);
lcd.print("Go to MM ");
lcd.setCursor(10,1);
virgola=passi ;
virgola=virgola/100;
lcd.print(virgola,2);
break;
case distanceCent:
lcd.setCursor(0,0);
lcd.print("Cicles to do > ");
lcd.setCursor(0,1);
lcd.print("Go to MM ");
lcd.setCursor(10,1);
virgola=passi ;
virgola=virgola/100;
lcd.print(virgola,2);
break;
case quanticicli:
lcd.setCursor(0,0);
lcd.print("Send & Run > ");
lcd.setCursor(0,1);
lcd.print("N of Cicles");
lcd.setCursor(12,1);
lcd.print((int)cicli-1);
break;
case jogmode:
virgola=passi ;
virgola=virgola/100;
if (virgola>=300)
{
lcd.print("Max Step Reached") ;
}
else
{
lcd.print("Jog < >");
}
lcd.setCursor(0,1);
lcd.print("MM - step");
lcd.setCursor(11,1);
lcd.print(virgola,2);
break;
}
return;
}
void trasmetti()
{
// ******** ENVOI ********
radio.stopListening(); // On commence par arrêter le mode écoute, pour pouvoir émettre les données
DatiRadioBUFF.cicliRADIO = cicli;
DatiRadioBUFF.passiRADIO = passi;
DatiRadioBUFF.velocitaRADIO = velocita ;
DatiRadioBUFF.accRADIO = acc ;
DatiRadioBUFF.AzzeraRadio= Azzera;
radio.write(&DatiRadioBUFF, sizeof(DatiRadioBUFF)); // … et on envoi cette valeur à l'autre arduino, via le NRF24
// Serial.println("cicli");
//Serial.println(cicli);
// Serial.println("passi");
// Serial.println(passi);
// Serial.println("velocita");
// Serial.println(velocita);
// Serial.println("acc");
// Serial.println(acc);
// Serial.println("Azzera");
// Serial.println(Azzera);
//delay(5);
return;
}
void ricevi()
{
// ******** RÉCEPTION ********
radio.openWritingPipe(adresses[0]); // Ouverture du "tunnel1" en ÉCRITURE
radio.openReadingPipe(1, adresses[1]); // Ouverture du "tunnel2" en LECTURE
radio.startListening();
// On commence par arrêter le mode envoi, pour pouvoir réceptionner des données
if(radio.available()) { // On regarde si une donnée a été reçue
while (radio.available()) { // Si une donné est en attente de lecture, on va la lire
radio.read(&DatiRadioBUFF, sizeof(DatiRadioBUFF));// radio.read(&cicli, sizeof(cicli)); // Lecture des données reçues, une par une
inutileDUE = DatiRadioBUFF.cicliRADIO;
// inutile = DatiRadioBUFF.velocitaRADIO;
//inutile = DatiRadioBUFF.accRADIO;
//inutile = DatiRadioBUFF.AzzeraRadio;
inutile = DatiRadioBUFF.passiRADIO;
}
delay(20); // avec une petite pause, avant de reboucler
}
delay(5);
return;
}
int get_real_key(void) // routine to return a valid keystroke
{
int trial_key = 0;
while (trial_key < 1)
{
trial_key = read_LCD_button();
}
delay(200); // 200 millisec delay between user keys
return trial_key;
}
int read_LCD_button() // routine to read the LCD's buttons
{
int key_in;
delay(ADSettleTime); // wait to settle
key_in = analogRead(0); // read ADC once
delay(ADSettleTime); // wait to settle
// average values for my board were: 0, 144, 324, 505, 742
// add approx 100 to those values to set range
if (key_in > 800) return NO_KEY;
if (key_in < 60) return RIGHT_KEY;
if (key_in < 200) return UP_KEY;
if (key_in < 400) return DOWN_KEY;
if (key_in < 600) return LEFT_KEY;
if (key_in < 800) return SELECT_KEY;
}
void lineariInterpolation()
{
Serial.begin(57600);
// if(Serial.available() && sw)
// {
// for(int j = 0; j < 3; j++)
// {
// input[j] = Serial.read();
// }
//command = ((String)input).toInt();
// sw = 0;
int Lstart;
int Lend;
int Lspeed;
int Lradius; // to tell the module it isn t a sfere
Serial.end();
}
void SfericalInterpolation()
{
Serial.begin(57600);
// if(Serial.available() && sw)
// {
// for(int j = 0; j < 3; j++)
// {
// input[j] = Serial.read();
// }
//command = ((String)input).toInt();
// sw = 0;
//}
// da mettere sul ricevitore
int Sstart;
int Send;
int Sspeed;
int Sradius;
int risol;
int fattore;
int gradi;
int X;
int radius;
fattore=360/ risol; //risoluzione coordinate
gradi=fattore*3.14/180;//convert to degrees per positon radians
for (int k = 0; k <=fattore-1; k++) {
gradi=gradi*k;
X=radius*cos(gradi);
//y=radius*sin(gradi);
}
// END da mettere sul ricevitore
Serial.end();
}