Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <CyberLib.h>
- #include <SPI.h>
- #include "nRF24L01.h"
- #include "RF24.h"
- #include <ServoTimer2.h>
- #define rfch 0x00
- #define tmax 1000
- #define dpin 9
- #define sdebug 1
- uint8_t m1 = 0;
- uint8_t m2 = 0;
- uint8_t m3 = 0;
- uint8_t m4 = 0;
- uint8_t scount = 4;
- uint32_t tm1 = 0;
- uint32_t tm2 = 0;
- uint32_t tm3 = 0;
- uint32_t b5 = 0;
- bool lr = 0;
- bool dual = 0;
- bool strobe = 0;
- bool light = 0;
- bool nodata;
- RF24 radio(7, 8);
- uint8_t address[][6] = {"z1", "z2", "z3", "z4", "z5", "z6"};
- ServoTimer2 servo;
- void setup() {
- //For ports 9,10 set PWM freq to 62500 Hz
- TCCR1A = TCCR1A & 0xe0 | 1;
- TCCR1B = TCCR1B & 0xe0 | 0x09;
- D2_Out;
- D4_Out;
- D5_Out;
- D6_Out;
- D9_Out;
- D10_Out;
- D2_Low;
- D4_Low;
- D5_Low;
- D6_Low;
- D9_Low;
- D10_Low;
- if (sdebug) {
- Serial.begin(115200);
- }
- radio.begin();
- radio.setAutoAck(0);
- radio.setRetries(0,15);
- radio.enableAckPayload();
- radio.setPayloadSize(5);
- radio.openReadingPipe(1,address[0]);
- radio.setChannel(rfch);
- radio.setPALevel (RF24_PA_MAX); //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
- radio.setDataRate (RF24_250KBPS); //RF24_2MBPS, RF24_1MBPS, RF24_250KBPS
- radio.powerUp();
- radio.startListening();
- servo.attach(3);
- }
- void loop() {
- uint32_t last_time;
- uint8_t data[5] = {0};
- uint8_t pipeNo;
- int8_t dspeed = 0;
- uint16_t srvdeg = 90;
- uint8_t count = 0;
- while(true) {
- if (radio.available(&pipeNo)) {
- last_time = millis();
- nodata = 0;
- radio.read(&data, sizeof(data));
- srvdeg = map(data[2], 0, 255, 850, 2150);
- if (srvdeg > 1475 && srvdeg < 1516) {
- srvdeg = 1480;
- }
- if (data[1] > 127) {
- D4_Low;
- D5_High;
- if (data[1] < 134) {
- dspeed = 0;
- } else {
- data[1] = map(data[1], 134, 255, 180, 255);
- dspeed = map(data[1], 128, 255, 0, 255);
- }
- } else {
- D4_High;
- D5_Low;
- if (data[1] > 119) {
- dspeed = 0;
- } else {
- data[1] = map(data[1], 120, 0, 80, 0);
- dspeed = map(data[1], 128, 0, 0, 255);
- }
- }
- // Debug to serial
- if (sdebug == 1) {
- count++;
- Serial.print(count);
- Serial.print("\t");
- Serial.print(data[0]);
- Serial.print("\t");
- Serial.print(data[1]);
- Serial.print("\t");
- Serial.print(data[2]);
- Serial.print("\t");
- Serial.print(data[3]);
- Serial.print("\t");
- Serial.print(data[4]);
- Serial.print("\t");
- Serial.print("dspeed ");
- Serial.print(dspeed);
- Serial.print("\t");
- Serial.print("srvdeg ");
- Serial.println(srvdeg);
- }
- }
- if (millis() - last_time > tmax) {
- srvdeg = 1480;
- dspeed = 0;
- nodata = 1;
- if (D10_Read == 1) {
- D2_High;
- D6_High;
- D10_Low;
- } else {
- D2_Low;
- D6_Low;
- D10_High;
- }
- last_time = millis();
- if (sdebug == 1) {
- Serial.println("No data");
- }
- }
- analogWrite(dpin, dspeed);
- servo.write(srvdeg);
- if (bitRead(data[4], 5) == 1) {
- if (millis() - b5 > 500) {
- if (strobe == 1) {
- strobe = 0;
- } else if (light == 1) {
- strobe = 1;
- light = 0;
- } else {
- light = 1;
- }
- b5 = millis();
- }
- }
- stroboscope();
- lights();
- }
- }
- void lights() {
- if (strobe == 1 || nodata == 1) {
- return 0;
- }
- if (light == 1) {
- D2_High;
- D6_High;
- D10_High;
- } else {
- D2_Low;
- D6_Low;
- D10_Low;
- }
- }
- void stroboscope() {
- if (strobe == 0 || nodata == 1) {
- m1 = 0;
- m2 = 0;
- m3 = 0;
- m4 = 0;
- return 0;
- }
- if (m1 < scount) {
- if (m2 < 12) {
- if (millis() > tm1 + 30) {
- if (! lr) {
- D2_Inv;
- D6_Low;
- D10_Low;
- } else {
- D2_Low;
- D6_Inv;
- D10_Inv;
- }
- tm1 = millis();
- m2++;
- }
- } else {
- if (lr) {
- m1++;
- }
- lr = !lr;
- m2 = 0;
- }
- tm2 = millis();
- } else {
- if (m3 < scount) {
- if (m4 < 12) {
- if (millis() > tm3 + 30) {
- if (! dual) {
- D2_Inv;
- D6_Inv;
- D10_Low;
- } else {
- D2_Low;
- D6_Low;
- D10_Inv;
- }
- tm3 = millis();
- m4++;
- }
- } else {
- if (dual) {
- m3++;
- }
- dual = !dual;
- m4 = 0;
- }
- } else {
- m1 = 0;
- m3 = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement