Components and supplies
Arduino Nano R3
Arduino Micro
Project description
Code
Untitled file
arduino
1// ATmega328P 2// Arduino UNO, NANO 3// SPI 4// 13 - SCK, 12 - MISO, 11 - MOSI, 10 - SS 5 6// 7// 8// 9 10// 11volatile uint8_t master_arr [4]; 12// 13volatile uint8_t slave_arr [4]; 14// 15volatile int16_t countSPIb = -1; 16 17void slave_init(){ 18 // SPI SLAVE 19 DDRB|=(1<<PB4); // MISO 20 SPCR |= (1 << SPIE)|(1 << SPE); // SPI SLAVE 21} 22 23ISR (SPI_STC_vect) // SPI - 24{ 25 if (countSPIb < 0) { // "" 26 countSPIb++; // 27 SPDR = slave_arr [countSPIb]; // 28 return; // 29 } 30 31 master_arr [countSPIb] = SPDR; // 32 countSPIb++; // 33 SPDR = slave_arr [countSPIb]; // (+1 ) 34 35 if (countSPIb >= sizeof(master_arr)) { // 36 countSPIb = -1; // 37 } 38} // SPI - 39
Nano_final2.ino
arduino
1#include "spi_slave.h" 2 3volatile uint8_t filter = 0; // 4volatile uint32_t fDisk18 = 0; // /18 5volatile uint32_t bDisk18 = 0; // /18 6volatile uint32_t OLDfDisk18 = 0; // /18 7volatile uint32_t OLDbDisk18 = 0; // /18 8volatile uint32_t startTime = 0; // 9volatile uint32_t endTime = 0; // 1/18 10boolean buttonWasUp = true; // BTSet ? 11 12void setup (void) 13{ 14 slave_init(); // SPI SLAVE 15 pinMode (2, INPUT_PULLUP); // yellow hall 16 pinMode (3, INPUT_PULLUP); // green hall 17 pinMode (4, OUTPUT); // yellow LED !!! 18 pinMode (5, OUTPUT); // green LED !!! 19 // D 20 bitSet(PCICR, 2); 21 // 2 3 22 bitSet(PCMSK2, 2); 23 bitSet(PCMSK2, 3); 24 slave_arr [0] = 0; // / int8_t 25 slave_arr [1] = 0; // 26 slave_arr [2] = 255; // 27 slave_arr [3] = 0; // Y 28 pinMode (14, INPUT_PULLUP); // BT9 29 pinMode (15, INPUT_PULLUP); // BT10 30 pinMode (16, INPUT_PULLUP); // BT11 31 pinMode (17, INPUT_PULLUP); // BT12 32 pinMode (9, INPUT_PULLUP); // BT13 33 pinMode (8, INPUT_PULLUP); // BT14 34 pinMode (7, INPUT_PULLUP); // BT15 35 pinMode (6, INPUT_PULLUP); // BTSet 36} 37 38 39ISR (PCINT2_vect) { // D0..D7 40 41 // 42 if (!bitRead(slave_arr [1], 7)) { 43 bitWrite(PORTD, 4, bitRead(PIND, 2)); 44 bitWrite(PORTD, 5, bitRead(PIND, 3)); 45 } 46 47 filter = filter << 1; 48 bitWrite(filter, 0, bitRead(PIND, 2)); 49 filter = filter << 1; 50 bitWrite(filter, 0, bitRead(PIND, 3)); 51 52 // 2,3 B10110100 53 if (filter == B10110100) { 54 // 55 OLDfDisk18 = fDisk18; 56 // 57 fDisk18++; 58 endTime = millis() - startTime; 59 startTime = millis(); 60 } 61 62 // 2,3 B10000111 63 if (filter == B10000111) { 64 // 65 OLDbDisk18 = bDisk18; 66 // 67 bDisk18++; 68 endTime = millis() - startTime; 69 startTime = millis(); 70 } 71 72 // 73 if (endTime>255) endTime = 255; 74 75} 76 77void loop (void) 78{ 79 if (digitalRead (SS) == HIGH) countSPIb = -1; // 80 81 // 0 / 1 82 boolean buttonIsUp = digitalRead(6); 83 // (&&) ... 84 if (buttonWasUp && !buttonIsUp) { 85 delay(10); 86 // 87 buttonIsUp = digitalRead(6); 88 if (!buttonIsUp) { // ... 89 // ... ! 0 / 1 90 bitWrite(slave_arr [1], 7, !bitRead(slave_arr [1], 7)); 91 } 92 } 93 // 94 buttonWasUp = buttonIsUp; 95 96 // 97 bitWrite(slave_arr [1], 0, !digitalRead(14)); // BT9 98 bitWrite(slave_arr [1], 1, !digitalRead(15)); // BT10 99 bitWrite(slave_arr [1], 2, !digitalRead(16)); // BT11 100 bitWrite(slave_arr [1], 3, !digitalRead(17)); // BT12 101 bitWrite(slave_arr [1], 4, !digitalRead(9)); // BT13 102 bitWrite(slave_arr [1], 5, !digitalRead(8)); // BT14 103 bitWrite(slave_arr [1], 6, !digitalRead(7)); // BT15 104 105 // 106 slave_arr [3] = map(analogRead(A7),0,1023,1,254); 107 108 // 109 if (millis() - startTime > 255) { 110 slave_arr[0] = 0; 111 endTime = 255; 112 } 113 114 115 if (OLDfDisk18 != fDisk18) { // 116 OLDfDisk18 = fDisk18; // 117 if (endTime < slave_arr [3]) slave_arr [0] = 64; 118 else slave_arr [0] = map(endTime, 255, slave_arr [3], 0, 64); 119 } 120 121 if (OLDbDisk18 != bDisk18) { // 122 OLDbDisk18 = bDisk18; // 123 if (endTime < slave_arr [3]) slave_arr [0] = -64; 124 else slave_arr [0] = map(endTime, 255, slave_arr [3], 0, -64); 125 } 126 127 // 128 slave_arr [2] = endTime; 129} 130 131// 132// Serial.begin(9600); 133//long previousMillis = 0; // 134//long interval = 100; // 135/* 136 unsigned long currentMillis = millis(); 137 if (currentMillis - previousMillis > interval) { 138 // 139 previousMillis = currentMillis; 140 int8_t testik = slave_arr [0]; 141 Serial.println(slave_arr [3]); 142 } 143*/ 144
spi_slave.h
arduino
1// ATmega328P 2// Arduino UNO, NANO 3// SPI 4// 13 - SCK, 12 - MISO, 11 - MOSI, 10 - SS 5 6// 7// 8// 9 10// 11volatile uint8_t master_arr [4]; 12// 13volatile uint8_t slave_arr [4]; 14// 15volatile int16_t countSPIb = -1; 16 17void slave_init(){ 18 // SPI SLAVE 19 DDRB|=(1<<PB4); // MISO 20 SPCR |= (1 << SPIE)|(1 << SPE); // SPI SLAVE 21} 22 23ISR (SPI_STC_vect) // SPI - 24{ 25 if (countSPIb < 0) { // "" 26 countSPIb++; // 27 SPDR = slave_arr [countSPIb]; // 28 return; // 29 } 30 31 master_arr [countSPIb] = SPDR; // 32 countSPIb++; // 33 SPDR = slave_arr [countSPIb]; // (+1 ) 34 35 if (countSPIb >= sizeof(master_arr)) { // 36 countSPIb = -1; // 37 } 38} // SPI - 39
Nano_final2.ino
arduino
1#include "spi_slave.h" 2 3volatile uint8_t filter = 0; // 4volatile uint32_t fDisk18 = 0; // /18 5volatile uint32_t bDisk18 = 0; // /18 6volatile uint32_t OLDfDisk18 = 0; // /18 7volatile uint32_t OLDbDisk18 = 0; // /18 8volatile uint32_t startTime = 0; // 9volatile uint32_t endTime = 0; // 1/18 10boolean buttonWasUp = true; // BTSet ? 11 12void setup (void) 13{ 14 slave_init(); // SPI SLAVE 15 pinMode (2, INPUT_PULLUP); // yellow hall 16 pinMode (3, INPUT_PULLUP); // green hall 17 pinMode (4, OUTPUT); // yellow LED !!! 18 pinMode (5, OUTPUT); // green LED !!! 19 // D 20 bitSet(PCICR, 2); 21 // 2 3 22 bitSet(PCMSK2, 2); 23 bitSet(PCMSK2, 3); 24 slave_arr [0] = 0; // / int8_t 25 slave_arr [1] = 0; // 26 slave_arr [2] = 255; // 27 slave_arr [3] = 0; // Y 28 pinMode (14, INPUT_PULLUP); // BT9 29 pinMode (15, INPUT_PULLUP); // BT10 30 pinMode (16, INPUT_PULLUP); // BT11 31 pinMode (17, INPUT_PULLUP); // BT12 32 pinMode (9, INPUT_PULLUP); // BT13 33 pinMode (8, INPUT_PULLUP); // BT14 34 pinMode (7, INPUT_PULLUP); // BT15 35 pinMode (6, INPUT_PULLUP); // BTSet 36} 37 38 39ISR (PCINT2_vect) { // D0..D7 40 41 // 42 if (!bitRead(slave_arr [1], 7)) { 43 bitWrite(PORTD, 4, bitRead(PIND, 2)); 44 bitWrite(PORTD, 5, bitRead(PIND, 3)); 45 } 46 47 filter = filter << 1; 48 bitWrite(filter, 0, bitRead(PIND, 2)); 49 filter = filter << 1; 50 bitWrite(filter, 0, bitRead(PIND, 3)); 51 52 // 2,3 B10110100 53 if (filter == B10110100) { 54 // 55 OLDfDisk18 = fDisk18; 56 // 57 fDisk18++; 58 endTime = millis() - startTime; 59 startTime = millis(); 60 } 61 62 // 2,3 B10000111 63 if (filter == B10000111) { 64 // 65 OLDbDisk18 = bDisk18; 66 // 67 bDisk18++; 68 endTime = millis() - startTime; 69 startTime = millis(); 70 } 71 72 // 73 if (endTime>255) endTime = 255; 74 75} 76 77void loop (void) 78{ 79 if (digitalRead (SS) == HIGH) countSPIb = -1; // 80 81 // 0 / 1 82 boolean buttonIsUp = digitalRead(6); 83 // (&&) ... 84 if (buttonWasUp && !buttonIsUp) { 85 delay(10); 86 // 87 buttonIsUp = digitalRead(6); 88 if (!buttonIsUp) { // ... 89 // ... ! 0 / 1 90 bitWrite(slave_arr [1], 7, !bitRead(slave_arr [1], 7)); 91 } 92 } 93 // 94 buttonWasUp = buttonIsUp; 95 96 // 97 bitWrite(slave_arr [1], 0, !digitalRead(14)); // BT9 98 bitWrite(slave_arr [1], 1, !digitalRead(15)); // BT10 99 bitWrite(slave_arr [1], 2, !digitalRead(16)); // BT11 100 bitWrite(slave_arr [1], 3, !digitalRead(17)); // BT12 101 bitWrite(slave_arr [1], 4, !digitalRead(9)); // BT13 102 bitWrite(slave_arr [1], 5, !digitalRead(8)); // BT14 103 bitWrite(slave_arr [1], 6, !digitalRead(7)); // BT15 104 105 // 106 slave_arr [3] = map(analogRead(A7),0,1023,1,254); 107 108 // 109 if (millis() - startTime > 255) { 110 slave_arr[0] = 0; 111 endTime = 255; 112 } 113 114 115 if (OLDfDisk18 != fDisk18) { // 116 OLDfDisk18 = fDisk18; // 117 if (endTime < slave_arr [3]) slave_arr [0] = 64; 118 else slave_arr [0] = map(endTime, 255, slave_arr [3], 0, 64); 119 } 120 121 if (OLDbDisk18 != bDisk18) { // 122 OLDbDisk18 = bDisk18; // 123 if (endTime < slave_arr [3]) slave_arr [0] = -64; 124 else slave_arr [0] = map(endTime, 255, slave_arr [3], 0, -64); 125 } 126 127 // 128 slave_arr [2] = endTime; 129} 130 131// 132// Serial.begin(9600); 133//long previousMillis = 0; // 134//long interval = 100; // 135/* 136 unsigned long currentMillis = millis(); 137 if (currentMillis - previousMillis > interval) { 138 // 139 previousMillis = currentMillis; 140 int8_t testik = slave_arr [0]; 141 Serial.println(slave_arr [3]); 142 } 143*/ 144
Untitled file
arduino
1// ATmega328P 2// Arduino UNO, NANO 3// SPI 4// 13 - SCK, 12 - MISO, 11 - MOSI, 10 - SS 5 6// 7// 8// 9 10// 11volatile uint8_t master_arr [4]; 12// 13volatile uint8_t slave_arr [4]; 14// 15volatile int16_t countSPIb = -1; 16 17void slave_init(){ 18 // SPI SLAVE 19 DDRB|=(1<<PB4); // MISO 20 SPCR |= (1 << SPIE)|(1 << SPE); // SPI SLAVE 21} 22 23ISR (SPI_STC_vect) // SPI - 24{ 25 if (countSPIb < 0) { // "" 26 countSPIb++; // 27 SPDR = slave_arr [countSPIb]; // 28 return; // 29 } 30 31 master_arr [countSPIb] = SPDR; // 32 countSPIb++; // 33 SPDR = slave_arr [countSPIb]; // (+1 ) 34 35 if (countSPIb >= sizeof(master_arr)) { // 36 countSPIb = -1; // 37 } 38} // SPI - 39
spi_master.h
arduino
1// Arduino UNO, NANO, Leonardo 2// SPI 3// 13 - SCK, 12 - MISO, 11 - MOSI, 10 - SS 4// SCK, MI, MO, SS Leonardo 5// 6// 7// 8 9// 10uint8_t master_arr [4]; 11// 12uint8_t slave_arr [4]; 13 14void mater_init(){ 15 digitalWrite(SS, HIGH); // ensure SS stays high for now 16 SPI.begin (); 17 // 18 SPI.setClockDivider(SPI_CLOCK_DIV8); 19} 20 21void refreshSPI () // 22{ 23 digitalWrite(SS, LOW); // enable Slave Select 24 // "" slave_arr[] 25 SPI.transfer (0xFF); 26 // 27 delayMicroseconds (20); 28 // master_arr[] 29 for (uint8_t i = 0; i < sizeof(master_arr); i++) { 30 slave_arr[i] = SPI.transfer(master_arr[i]); 31 // 32 delayMicroseconds (20); 33 } 34 digitalWrite(SS, HIGH); // disable Slave Select 35} // end of refreshSPI 36
spi_master.h
arduino
1// Arduino UNO, NANO, Leonardo 2// SPI 3// 13 - SCK, 12 - MISO, 4 11 - MOSI, 10 - SS 5// SCK, MI, MO, SS Leonardo 6// 7// 8// 9 10 11// 12uint8_t master_arr [4]; 13// 14uint8_t slave_arr 15 [4]; 16 17void mater_init(){ 18 digitalWrite(SS, HIGH); // ensure SS stays 19 high for now 20 SPI.begin (); 21 // 22 SPI.setClockDivider(SPI_CLOCK_DIV8); 23} 24 25void 26 refreshSPI () // 27{ 28 digitalWrite(SS, LOW); // enable Slave 29 Select 30 // "" slave_arr[] 31 SPI.transfer (0xFF); 32 // 33 34 delayMicroseconds (20); 35 // master_arr[] 36 for (uint8_t i = 0; i < sizeof(master_arr); 37 i++) { 38 slave_arr[i] = SPI.transfer(master_arr[i]); 39 // 40 delayMicroseconds 41 (20); 42 } 43 digitalWrite(SS, HIGH); // disable Slave Select 44} 45 // end of refreshSPI 46
Leonardo_final2.ino
arduino
1#include <SPI.h> 2#include <Wire.h> 3#include "spi_master.h" 4#include <Adafruit_GFX.h> 5#include <Adafruit_SSD1306.h> 6#include <Joystick.h> 7 8Adafruit_SSD1306 display(128, 32, &Wire, -1); 9 10 11Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 12 15, 0, // Button Count, Hat Switch Count 13 true, true, false, // X and Y, but no Z Axis 14 false, false, false, // No Rx, Ry, or Rz 15 false, false, // No rudder or throttle 16 false, false, false); // No accelerator, brake, or steering 17 18 19int16_t X = 0; // 20int16_t XL = 0; // 21int16_t XR = 0; // 22int16_t XF = 0; // X () 23int16_t XLm = 490; // 24int16_t XRm = 500; // 25int16_t wheelAngle = 0; // 26 27int8_t Y = 0; 28 29// , , 30uint8_t hours = 0; 31uint8_t minutes = 0; 32uint8_t seconds = 0; 33// long 34long previousMillis = 0; // 35long interval = 1000; // / (1 ) 36long lastTime = 0; 37 38void setup (void) 39{ 40 mater_init(); 41 // 42 Serial.begin (9600); 43 Serial.println (); 44 // 45 pinMode(4, INPUT_PULLUP); 46 pinMode(5, INPUT_PULLUP); 47 pinMode(6, INPUT_PULLUP); 48 pinMode(7, INPUT_PULLUP); 49 pinMode(8, INPUT_PULLUP); 50 pinMode(9, INPUT_PULLUP); 51 pinMode(10, INPUT_PULLUP); 52 pinMode(11, INPUT_PULLUP); 53 // ( - ) 54 pinMode(13, OUTPUT); 55 56 master_arr [0] = 0; // n/a 57 master_arr [1] = 0; // n/a 58 master_arr [2] = 0; // n/a 59 60 // ( ) 61 Joystick.begin(false); 62 Joystick.setYAxisRange(-64, 64); 63 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 64} 65 66 67void loop (void) 68{ 69 refreshSPI (); 70 71 unsigned long currentMillis = millis(); 72 73 // , 74 if (currentMillis - previousMillis > interval) { 75 // 76 previousMillis = currentMillis; 77 78 //... lastTime +1 79 lastTime = millis(); 80 seconds++; 81 // 60, +1... 82 if (seconds >= 60) { 83 seconds = 0; 84 minutes++; 85 } 86 // ... ... 87 if (minutes >= 60) { 88 minutes = 0; 89 hours++; 90 } 91 // ... 92 if (hours >= 24) { 93 hours = 0; 94 } 95 } 96 97 // 98 if (!digitalRead(7)&&!digitalRead(4)) { 99 seconds = 0; 100 minutes = 0; 101 hours = 0; 102 } 103 104 105 // / 106 digitalWrite(13, bitRead(slave_arr [1], 7)); 107 108 wheelAngle = map(analogRead(A5), 2, 1020, 90, 20); 109 wheelAngle = wheelAngle * 10; 110 111 Joystick.setXAxisRange(-wheelAngle / 2, wheelAngle / 2); 112 113 X = map(analogRead(A0), 0, 1023, 1023, 0); 114 115 XL = map(analogRead(A1), 470, 720, 0, 350); 116 if (XL < 0) XL = 0; 117 if (XL > 350) XL = 350; 118 119 XR = map(analogRead(A2), 580, 340, 0, 350); 120 if (XR < 0) XR = 0; 121 if (XR > 350) XR = 350; 122 123 if (X >= XLm && X <= XRm) { 124 // 125 XF = 0; 126 } else if (X > XRm) { 127 // 128 int16_t x = X - XRm; 129 if (x > 300)x = 300; 130 XF = x / 3; 131 } else if (X < XLm) { 132 // 133 int16_t x = X - XLm; 134 if (x < -292) x = -300; 135 XF = x / 3; 136 } 137 138 XF = XF + XR - XL; 139 140 Y = slave_arr [0]; 141 142 // 143 if (!digitalRead(10)) Joystick.pressButton (0); else Joystick.releaseButton (0); // rudder BT1 144 if (!digitalRead(11)) Joystick.pressButton (1); else Joystick.releaseButton (1); // rudder BT2 145 if (!digitalRead(9)) Joystick.pressButton (2); else Joystick.releaseButton (2); // rudder BT3 146 if (!digitalRead(8)) Joystick.pressButton (3); else Joystick.releaseButton (3); // rudder BT4 147 if (!digitalRead(7)) Joystick.pressButton (4); else Joystick.releaseButton (4); // rudder BT5 148 if (!digitalRead(5)) Joystick.pressButton (5); else Joystick.releaseButton (5); // rudder BT6 149 if (!digitalRead(6)) Joystick.pressButton (6); else Joystick.releaseButton (6); // rudder BT7 150 if (!digitalRead(4)) Joystick.pressButton (7); else Joystick.releaseButton (7); // rudder BT8 151 152 if (bitRead(slave_arr [1], 0)) Joystick.pressButton (8); else Joystick.releaseButton (8); // rudder BT9 153 if (bitRead(slave_arr [1], 1)) Joystick.pressButton (9); else Joystick.releaseButton (9); // rudder BT10 154 if (bitRead(slave_arr [1], 2)) Joystick.pressButton (10); else Joystick.releaseButton (10); // rudder BT11 155 if (bitRead(slave_arr [1], 3)) Joystick.pressButton (11); else Joystick.releaseButton (11); // rudder BT12 156 if (bitRead(slave_arr [1], 4)) Joystick.pressButton (12); else Joystick.releaseButton (12); // rudder BT13 157 if (bitRead(slave_arr [1], 5)) Joystick.pressButton (13); else Joystick.releaseButton (13); // rudder BT14 158 if (bitRead(slave_arr [1], 6)) Joystick.pressButton (14); else Joystick.releaseButton (14); // rudder BT15 159 160 Joystick.setXAxis(XF); 161 Joystick.setYAxis(Y); 162 163 164 Serial.println(XL); 165 //Serial.print('='); 166 //Serial.println(XF); 167 168 169 // 170 display.clearDisplay(); 171 display.setTextSize(2); // Normal 1:1 pixel scale 172 display.setTextColor(SSD1306_WHITE); // Draw white text 173 display.setCursor(0, 0); // Start at top-left corner 174 display.print(XF); 175 display.setCursor(64, 0); // Start at top-left corner 176 display.print(Y); 177 178 if (!digitalRead(13)) { 179 // - 180 // X 181 display.setTextSize(1); 182 display.setCursor(0, 16); 183 display.print (wheelAngle); 184 // Y 185 display.setCursor(64, 16); 186 // 187 display.print(slave_arr [2]); 188 // 189 display.setCursor(64, 24); 190 display.print(slave_arr [3]); 191 } else { 192 // - 193 display.setTextSize(2); 194 display.setCursor(0, 16); 195 196 if (hours < 10) display.print(0); 197 display.print(hours); 198 display.print(':'); 199 200 if (minutes < 10) display.print(0); 201 display.print(minutes); 202 display.print(':'); 203 204 if (seconds < 10) display.print(0); 205 display.print(seconds); 206 } 207 208 display.display(); 209 210 // 211 Joystick.sendState(); 212} 213
spi_slave.h
arduino
1// ATmega328P 2// Arduino UNO, NANO 3// SPI 4// 13 5 - SCK, 12 - MISO, 11 - MOSI, 10 - SS 6 7// 8// 9// 10 11// 12 13volatile uint8_t master_arr [4]; 14// 15volatile uint8_t slave_arr 16 [4]; 17// 18volatile int16_t countSPIb = -1; 19 20void slave_init(){ 21 22 // SPI SLAVE 23 DDRB|=(1<<PB4); // MISO 24 SPCR 25 |= (1 << SPIE)|(1 << SPE); // SPI SLAVE 26} 27 28ISR (SPI_STC_vect) 29 // SPI - 30{ 31 if (countSPIb < 0) { // 32 "" 33 countSPIb++; // 34 SPDR = slave_arr 35 [countSPIb]; // 36 return; // 37 38 } 39 40 master_arr [countSPIb] = SPDR; // 41 countSPIb++; // 42 43 SPDR = slave_arr [countSPIb]; // (+1 ) 44 45 if (countSPIb 46 >= sizeof(master_arr)) { // 47 countSPIb = -1; // 48 49 } 50} // SPI - 51
Comments
Only logged in users can leave comments