// Tactile8TX.ino // // Gerard Paresys // 11 9 2023 // // Utilise la library Adafruit_DRV2605 // https://github.com/adafruit/Adafruit_DRV2605_Library/tree/master/examples/realtime // Utilise la library Adafruit BusIO // Utilise la library Adafruit RadioHead // https://github.com/adafruit/RadioHead // https://github.com/adafruit/RadioHead/tree/master/examples/feather/RadioHead69_RawDemo_TX // // OK Adafruit Feather M0 Radio + ADAFRUIT DRV2605L // // Pushbutton red from Pin 5 to ground // Pushbutton yellow from Pin 6 to ground // Pushbutton green from Pin 9 to ground // // the real-time playback mode allows the host processor to bypass the library playback engine // and play waveforms directly from the host through the I2C. // The real-time playback mode is a simple, single 8-bit register interface that holds an amplitude value. // When real- time playback is enabled, the real-time playback register is sent directly to the playback engine. // This value is played until the user sends the device to standby mode or removes the device from RTP mode. // The RTP mode operates exactly like the PWM mode except that the user enters a register value over the I2C // rather than a duty cycle through the input pin. // Therefore, any API (application-programming interface) designed for use with a PWM generator // in the host processor can write the data values over the I2C rather than writing the data values // to the host timer. // This ability frees a timer in the host while retaining compatibility with the original software. // For the LRA, the DRV2605 device automatically tracks the resonance frequency. // https://cdn-learn.adafruit.com/assets/assets/000/113/382/original/drv2605l.pdf // // http://adafruit.github.io/Adafruit_DRV2605_Library/html/class_adafruit___d_r_v2605.html #include #include "Adafruit_DRV2605.h" #include #include Adafruit_DRV2605 drv; // ************ Radio Setup *************** #define RF69_FREQ 868.0 // 868MHz (EU) ou 915MHz (US) #define RFM69_CS 8 #define RFM69_INT 3 #define RFM69_RST 4 #define LED 13 // Singleton instance of the radio driver RH_RF69 rf69(RFM69_CS, RFM69_INT); int16_t packetnum = 0; // packet counter, we increment per xmission const int buttonRedPin = 5; const int buttonYellowPin = 6; const int buttonGreenPin = 9; int buttonRedState = 0; // variable for reading the pushbutton status int buttonYellowState = 0; // variable for reading the pushbutton status int buttonGreenState = 0; // variable for reading the pushbutton status int buttonRedStateAncien = 0; // variable for reading the pushbutton status int buttonYellowStateAncien = 0; // variable for reading the pushbutton status int buttonGreenStateAncien = 0; // variable for reading the pushbutton status char RadioMessage[20] = "buttonRedOff"; void setup() { Serial.begin(9600); drv.begin(); drv.setMode(DRV2605_MODE_REALTIME); // Set Real-Time Playback mode // drv.useERM(); // Use ERM (Eccentric Rotating Mass) mode. drv.useLRA(); // Use LRA (Linear Resonance Actuator) mode. pinMode(buttonRedPin, INPUT_PULLUP); // initialize the pushbutton pin as an input pinMode(buttonYellowPin, INPUT_PULLUP); // initialize the pushbutton pin as an input pinMode(buttonGreenPin, INPUT_PULLUP); // initialize the pushbutton pin as an input pinMode(LED, OUTPUT); drv.setRealtimeValue(0x00); // nul pinMode(RFM69_RST, OUTPUT); digitalWrite(RFM69_RST, LOW); Serial.println("Sketch Tactile6.ino"); Serial.println(); // manual reset digitalWrite(RFM69_RST, HIGH); delay(10); digitalWrite(RFM69_RST, LOW); delay(10); if (!rf69.init()) { Serial.println("RFM69 radio init failed"); while (1); } Serial.println("RFM69 radio init OK!"); // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module) // No encryption if (!rf69.setFrequency(RF69_FREQ)) { Serial.println("setFrequency failed"); } // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the // ishighpowermodule flag set like this: rf69.setTxPower(20, true); // range from 14-20 for power, 2nd arg must be true for 69HCW // The encryption key has to be the same as the one in the server uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; rf69.setEncryptionKey(key); Serial.print("RFM69 radio @"); Serial.print((int)RF69_FREQ); Serial.println(" MHz"); } void loop() { buttonRedState = digitalRead(buttonRedPin); // read the state of the pushbutton buttonYellowState = digitalRead(buttonYellowPin); // read the state of the pushbutton buttonGreenState = digitalRead(buttonGreenPin); // read the state of the pushbutton if (buttonRedState != buttonRedStateAncien) { if (buttonRedState == LOW) { drv.setRealtimeValue(0x7F); // Vibreur Maxi digitalWrite(LED, HIGH); // LED on: char RadioMessage[20] = "buttonRedOn"; SendRadioMessage(RadioMessage); } else { drv.setRealtimeValue(0x00); // Vibreur nul digitalWrite(LED, LOW); // LED off: char RadioMessage[20] = "buttonRedOff"; SendRadioMessage(RadioMessage); } buttonRedStateAncien = buttonRedState; Serial.print("buttonRed "); Serial.println(buttonRedState); } if (buttonYellowState != buttonYellowStateAncien) { if (buttonYellowState == LOW) { drv.setRealtimeValue(0x2F); // Vibreur Moyen digitalWrite(LED, HIGH); // LED on: char RadioMessage[20] = "buttonYellowOn"; SendRadioMessage(RadioMessage); } else { drv.setRealtimeValue(0x00); // Vibreur nul digitalWrite(LED, LOW); // LED off: char RadioMessage[20] = "buttonYellowOff"; SendRadioMessage(RadioMessage); } buttonYellowStateAncien = buttonYellowState; Serial.print("buttonYellow "); Serial.println(buttonYellowState); } if (buttonGreenState != buttonGreenStateAncien) { if (buttonGreenState == LOW) { drv.setRealtimeValue(0x17); // Vibreur Faible digitalWrite(LED, HIGH); // LED on: char RadioMessage[20] = "buttonGreenOn"; SendRadioMessage(RadioMessage); } else { drv.setRealtimeValue(0x00); // Vibreur nul digitalWrite(LED, LOW); // LED off: char RadioMessage[20] = "buttonGreenOff"; SendRadioMessage(RadioMessage); } buttonGreenStateAncien = buttonGreenState; Serial.print("buttonGreen "); Serial.println(buttonGreenState); } if (rf69.available()) { // Should be a message for us now uint8_t buf[RH_RF69_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); if (rf69.recv(buf, &len)) { if (!len) return; buf[len] = 0; Serial.print("Received ["); Serial.print(len); Serial.print("]: "); Serial.println((char*)buf); Serial.print("RSSI: "); Serial.println(rf69.lastRssi(), DEC); // RSSI: Receiver Signal Strength Indicator -15 .. -80 if (strstr((char *)buf, "buttonRedOn")) { Blink(LED, 10, 1); // blink LED 1 time, 10ms between blinks drv.setRealtimeValue(0x7F); // Vibreur Maxi } if (strstr((char *)buf, "buttonRedOff")) { Blink(LED, 10, 1); // blink LED 1 time, 10ms between blinks drv.setRealtimeValue(0x00); // Vibreur nul } } else { Serial.println("Receive failed"); } } delay(10); } void SendRadioMessage(char radiopacket[20]) { itoa(packetnum++, radiopacket + 16, 10); // itoa(packetnum++, radiopacket + 13, 10); Serial.print("Sending "); Serial.println(radiopacket); // Send a message! rf69.send((uint8_t *)radiopacket, strlen(radiopacket)); rf69.waitPacketSent(); } void Blink(byte pin, byte delay_ms, byte loops) { // while (loops--) { // digitalWrite(pin, HIGH); // delay(delay_ms); // digitalWrite(pin, LOW); // delay(delay_ms); // } }