Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
EmaMaker | 52d3fac821 | |
EmaMaker | 143da9ed0a | |
EmaMaker | 28819f62d0 | |
EmaMaker | 15658c9d3c | |
EmaMaker | b115c0dfd5 | |
EmaMaker | a8323fde5b |
|
@ -1,4 +1 @@
|
|||
Code Samples
|
||||
|
||||
Code in this branch are useful in every language and can be easily translated in other languages.
|
||||
Just read the concept, not the code
|
||||
Codes here are only used in Arduino-C language, with arduino specific codes (interrupts, spi, etc...)
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
// Written by Nick Gammon
|
||||
// February 2011
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
void setup (void)
|
||||
{
|
||||
|
||||
//10 is the SS pin. Not the Gestapo one
|
||||
|
||||
Serial.begin(9600);
|
||||
pinMode(10, OUTPUT);
|
||||
digitalWrite(10, HIGH); // ensure 10 stays high for now
|
||||
|
||||
// Put SCK, MOSI, 10 pins into output mode
|
||||
// also put SCK, MOSI into LOW state, and 10 into HIGH state.
|
||||
// Then put SPI hardware into Master mode and turn SPI on
|
||||
SPI.begin ();
|
||||
|
||||
// Slow down the master a bit
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV8);
|
||||
|
||||
} // end of setup
|
||||
|
||||
|
||||
void loop (void)
|
||||
{
|
||||
|
||||
char c;
|
||||
|
||||
// enable Slave Select
|
||||
digitalWrite(10, LOW); // 10 is pin 10
|
||||
|
||||
// send test string
|
||||
for (const char * p = "Hello, world!\n" ; c = *p; p++){
|
||||
Serial.print(c);
|
||||
SPI.transfer (c);
|
||||
}
|
||||
// disable Slave Select
|
||||
digitalWrite(10, HIGH);
|
||||
|
||||
delay (1000); // 1 seconds delay
|
||||
} // end of loop
|
|
@ -0,0 +1,57 @@
|
|||
// Written by Nick Gammon
|
||||
// February 2011
|
||||
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
char buf [100];
|
||||
volatile byte pos;
|
||||
volatile boolean process_it;
|
||||
|
||||
void setup (void)
|
||||
{
|
||||
Serial.begin (115200); // debugging
|
||||
|
||||
// turn on SPI in slave mode
|
||||
SPCR |= bit (SPE);
|
||||
|
||||
// have to send on master in, *slave out*
|
||||
pinMode(MISO, OUTPUT);
|
||||
|
||||
// get ready for an interrupt
|
||||
pos = 0; // buffer empty
|
||||
process_it = false;
|
||||
|
||||
// now turn on interrupts
|
||||
SPI.attachInterrupt();
|
||||
|
||||
} // end of setup
|
||||
|
||||
|
||||
// SPI interrupt routine
|
||||
ISR (SPI_STC_vect)
|
||||
{
|
||||
byte c = SPDR; // grab byte from SPI Data Register
|
||||
|
||||
// add to buffer if room
|
||||
if (pos < (sizeof (buf) - 1))
|
||||
buf [pos++] = c;
|
||||
|
||||
// example: newline means time to process buffer
|
||||
if (c == '\n')
|
||||
process_it = true;
|
||||
|
||||
} // end of interrupt routine SPI_STC_vect
|
||||
|
||||
// main loop - wait for flag set in interrupt routine
|
||||
void loop (void)
|
||||
{
|
||||
if (process_it)
|
||||
{
|
||||
buf [pos] = 0;
|
||||
Serial.println (buf);
|
||||
pos = 0;
|
||||
process_it = false;
|
||||
} // end of flag set
|
||||
|
||||
} // end of loop
|
|
@ -0,0 +1,22 @@
|
|||
#include <SoftwareSerial.h>
|
||||
|
||||
SoftwareSerial mySerial(6, 7); // RX, TX
|
||||
|
||||
/**THIS EXAMPLE HAS BEEN SET UP AS MASTER-SLAVE COMMUNICATION, BUT USING mySerial.listen() and mySerial.send() BOTH ARDUINO'S CAN SEND DATA TO THE OTHER**/
|
||||
|
||||
void setup() {
|
||||
// Begin the Serial at 9600 Baud
|
||||
Serial.begin(9600);
|
||||
|
||||
|
||||
Serial.println("Master arduino ready to send!");
|
||||
|
||||
// set the data rate for the SoftwareSerial port
|
||||
mySerial.begin(57600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
mySerial.write('A'); //Write the serial data: A is corrispondent for 65 (ASCII)
|
||||
delay(1); //bit of delay for the data to be processed
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <SoftwareSerial.h>
|
||||
|
||||
SoftwareSerial mySerial(6, 7); // RX, TX
|
||||
|
||||
/**THIS EXAMPLE HAS BEEN SET UP AS MASTER-SLAVE COMMUNICATION, BUT USING mySerial.listen() and mySerial.send() BOTH ARDUINO'S CAN SEND DATA TO THE OTHER**/
|
||||
|
||||
void setup() {
|
||||
// Begin the Serial at 9600 Baud
|
||||
Serial.begin(9600);
|
||||
|
||||
Serial.println("Slave Arduino ready to receive!");
|
||||
|
||||
// set the data rate for the SoftwareSerial port
|
||||
mySerial.begin(57600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//prints what the master sent, if data is available
|
||||
if(mySerial.available() > 0){
|
||||
Serial.println(mySerial.read());
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
'''
|
||||
PYTHON CODE BRUTEFORCES WORDS FROM GIVEN CHAR LIST.
|
||||
ENDLESS WORD LIST CAN BE GENERATED, UNTIL PROGRAM STOPPED
|
||||
EVERY 100 WORDS GENERATED THEY ARE SAVED ON FILE
|
||||
'''
|
||||
|
||||
|
||||
lPossibleChars = ("a", "b", "c", "d", "e", "f")
|
||||
sWord = ""
|
||||
sFileName = "words.txt"
|
||||
lGenWords = []
|
||||
dWordsForSave = 100
|
||||
|
||||
def bruteforce(index):
|
||||
global sWord, lPossibleChars
|
||||
|
||||
#generates only if the index to change is less than currrent word length
|
||||
if index < len(sWord):
|
||||
for i in range(len(lPossibleChars)):
|
||||
|
||||
#makes the word a list
|
||||
lWord = list(sWord)
|
||||
#changes the index
|
||||
lWord[index] = lPossibleChars[i]
|
||||
#makes the list back a word
|
||||
sWord = ''.join(lWord)
|
||||
|
||||
print(sWord)
|
||||
#adds to the list
|
||||
lGenWords.append(sWord)
|
||||
|
||||
#goes on for the next index
|
||||
bruteforce(index + 1)
|
||||
|
||||
|
||||
while True:
|
||||
#saves word on file (append mode) and cleares the list
|
||||
if len(lGenWords) >= dWordsForSave:
|
||||
f = open(sFileName, "a")
|
||||
|
||||
for s in lGenWords:
|
||||
f.write(s + "\n")
|
||||
|
||||
lGenWords[:] = []
|
||||
f.close()
|
||||
|
||||
#starts bruteforcing the word and adds a new character when ended
|
||||
if len(sWord) > 0:
|
||||
bruteforce(0)
|
||||
sWord += lPossibleChars[0]
|
|
@ -0,0 +1,89 @@
|
|||
// Written by Nick Gammon
|
||||
// February 2011
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
#define DISTANCE 0b00001111
|
||||
#define DEGREES 0b00001010
|
||||
|
||||
#define SPI_DELAY 150
|
||||
#define SS 10
|
||||
|
||||
SPISettings settings(100000, MSBFIRST, SPI_MODE0);
|
||||
byte mess;
|
||||
|
||||
void setup (void)
|
||||
{
|
||||
|
||||
//10 is the SS pin. Not the Gestapo one
|
||||
|
||||
Serial.begin(9600);
|
||||
pinMode(SS, OUTPUT);
|
||||
digitalWrite(SS, HIGH); // ensure 10 stays high for now
|
||||
|
||||
// Put SCK, MOSI, 10 pins into output mode
|
||||
// also put SCK, MOSI into LOW state, and 10 into HIGH state.
|
||||
// Then put SPI hardware into Master mode and turn SPI on
|
||||
SPI.begin ();
|
||||
|
||||
// Slow down the master a bit
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV8);
|
||||
|
||||
} // end of setup
|
||||
|
||||
|
||||
void loop (void) {
|
||||
|
||||
/**THIS PART COULD BE REFACTORED INTO A NICER THING, BUT IT'S JUST FOR EXAMPLE**/
|
||||
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Sending byte: ");
|
||||
Serial.println(DISTANCE);
|
||||
|
||||
//Sends a byte to the slave. The slave now prepares the response byte (The slave knows what to do)
|
||||
SPI.beginTransaction(settings);
|
||||
digitalWrite(SS, LOW);
|
||||
SPI.transfer(DISTANCE);
|
||||
digitalWrite(SS, HIGH);
|
||||
SPI.endTransaction();
|
||||
delay(SPI_DELAY);
|
||||
|
||||
//Sends a byte to get the response that the slave has prepared
|
||||
Serial.print("Now getting response for previously asked byte: ");
|
||||
SPI.beginTransaction(settings);
|
||||
digitalWrite(SS, LOW);
|
||||
mess = SPI.transfer(0);
|
||||
digitalWrite(SS, HIGH);
|
||||
SPI.endTransaction();
|
||||
|
||||
Serial.println(mess);
|
||||
delay(SPI_DELAY);
|
||||
|
||||
|
||||
//Does the same thing but with different bytes
|
||||
Serial.println();
|
||||
Serial.print("Sending byte: ");
|
||||
Serial.println(DEGREES);
|
||||
|
||||
SPI.beginTransaction(settings);
|
||||
digitalWrite(SS, LOW);
|
||||
SPI.transfer(DEGREES);
|
||||
digitalWrite(SS, HIGH);
|
||||
SPI.endTransaction();
|
||||
delay(SPI_DELAY);
|
||||
|
||||
|
||||
Serial.print("Now getting response for previously asked byte: ");
|
||||
SPI.beginTransaction(settings);
|
||||
digitalWrite(SS, LOW);
|
||||
mess = SPI.transfer(0);
|
||||
digitalWrite(SS, HIGH);
|
||||
SPI.endTransaction();
|
||||
|
||||
Serial.println(mess);
|
||||
delay(SPI_DELAY);
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include <SPI.h>
|
||||
|
||||
#define DISTANCE 0b00001111
|
||||
#define DEGREES 0b00001010
|
||||
|
||||
byte c;
|
||||
bool bSending = false;
|
||||
byte bSendingByte;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
SPCR |= bit (SPE);
|
||||
|
||||
pinMode(MISO, OUTPUT);
|
||||
SPI.attachInterrupt();
|
||||
|
||||
}
|
||||
|
||||
ISR(SPI_STC_vect ) {
|
||||
//the bool defines whether the slave is sending or not.
|
||||
//It's assumed by both master and slave that the first communication is from master to slave
|
||||
if(bSending){
|
||||
//sends the previously prepared byte to the master
|
||||
SPCR = 64 & 127; // disble interrupt
|
||||
SPDR= bSendingByte;
|
||||
SPCR = 64 |128; // enable interrupt
|
||||
Serial.print("Sending byte: ");
|
||||
Serial.println(bSendingByte);
|
||||
}else{
|
||||
//prepares output for next time it's asked, based on the received byte
|
||||
switch(SPDR){
|
||||
case DISTANCE:
|
||||
bSendingByte = 0b00111111;
|
||||
break;
|
||||
case DEGREES:
|
||||
bSendingByte = 0b11100011;
|
||||
break;
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Received byte: ");
|
||||
Serial.print(SPDR);
|
||||
Serial.println(" - Preparing response byte");
|
||||
|
||||
//sets SPDR (SPI byte) to be send next time
|
||||
SPDR = bSendingByte;
|
||||
}
|
||||
|
||||
//Negates bSesnding. If the slave has received, next time he will send and viceversa
|
||||
bSending = !bSending;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#define BTN 5
|
||||
|
||||
void setup() {
|
||||
|
||||
attachInterrupt(BTN, testInterrupt, RISING);
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//Serial.println(digitalRead(5));
|
||||
Serial.println("ciao");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void testInterrupt(){
|
||||
for(int i = 0; i < 10; i++){
|
||||
Serial.println("TEST INTERRUPT");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue