battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen their skills
 
HomeGallerySearchRegisterLog in
 

 Arduino Grimoire

Go down 
Go to page : 1, 2  Next
AuthorMessage
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 8:56 pm

begginer course



pins :
rx - receive data?
tx - transmits data
~ pins : can vary or read voltage ranges of 5v
a0->5 : analog pin headers : converts analog to digital signals
power headers : 3.3v , 5v. this will output voltage
reset btn : will reset to the begining of the arduino program
or connect the reset pin header to 0v.

downlad the audrina IDE :
https://www.arduino.cc/en/software
it will install on :
C:\Users\username\AppData\Local\Programs\Arduino IDE
^ for the beta ver
or on
C:\Program Files (x86)\Arduino
for the 1.8 version
file, preferences, choose a folder to store your audrino sketches (.ino files)
and check the verify code option on.
################################################################
arduino sketch names cant have spaces in them

ctrl+r or the check btn : check sketch for errors
ctrl+u or -> btn : UL code to arduino
shift+ctrl+m : serial monitor window // allows you to see communication between
computer and arudino.
drop down menu : add tabs
#######################################
file, examples : see ready made example sketches

https://www.tinkercad.com/
circuits has arduino simulators, where you can test sketches

note pin 13 is connected to a leg, which is usefull for fast testing.

###############################################################################################
arduino code

comments :
//comment
/*multi line comment*/

a typical arduino program skelton :

hello world example :

Code:
/*
  This program blinks pin 13 of the Arduino (the
  built-in LED)
*/
// global vars here
void setup()
{
//1st function to run. here you handle global variables and stuff
  pinMode(13, OUTPUT);//set pin number as an output
}

void loop()
{
// this function runs forever till the arduino is shut down
  // turn the LED on (HIGH is the voltage level)
  digitalWrite(13, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  // turn the LED off by making the voltage LOW
  digitalWrite(13, LOW); // LOW = 0v
  delay(1000); // Wait for 1000 millisecond(s)
}

the above codes blinks the led (on board led or led connected to pin 13 and ground)

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 8:58 pm

declaring variables :
int led = 13;
Unsigned int counter =60;//positive ints with double the range, up to 2^32-1
Boolean state = true;
Char chr_a = 'a';
Char c2 = 97;//see ascii chart
Unsigned Char chr_y =121;//byte type var but saves as a char, 'y' in this case:
byte m = 25;
word w = 1000;//16bit unsigned # on arduino uno or 32 bit on the Due and zero board
Long velocity = 103002;
Unsigned Long velocity = 223002;//bigger and positive #
short val = 13;//like int
float num = 1.463;
double num = 45.888;//have bigger range on due boards

operators :
-+=*/
mod : %
and :&& or: || not: !

compound operators :
++ : A++ will increament A by 1
--, +=,-=.*=./=,%=
control statements : the same as java
loops and terner expression : same as java
functions : same as java
strings :

Code:
void setup() {
  // put your setup code here, to run once:
  char like[]="I like coffee and cake";//create string
  Serial.begin(9600);//won't work without it
  Serial.println(like);
  //delete part of string :
  like[13]=0;
  Serial.println(like);
  like[13]=' ';
  like[18]='t';
   like[19]='e';
  like[20]='a';
  like[21]=0;
  Serial.println(like);
}

void loop() {
  // put your main code here, to run repeatedly:

}

output :
I like coffee and cake
I like coffee
I like coffee and tea

int num = strlen(str);//string length
num = sizeof(str);//array size
stropy(out_str,str);//copy string
strcat(out_str," sketch");//concats string to out_str
str.toUpperCase();
str.replace("hello","");
str.length();//get length

note : string arrays take much less space than strings. space is an issue with arduinos

TIME functions :

delay(miliseconds);//pause the thread
delayMicroSeconds(x);
millis();//returns millis from program start time
micros();//returns micro secs from program start, but overflows and returns to 0 past 70 minutes

MATH LIBRARY

double cos (double __x) // returns cosine of x
double fabs (double __x) // absolute value of a float
double fmod (double __x, double __y) // floating point modulo
double modf (double __value, double *__iptr) // breaks the argument value into
// integral and fractional parts
double sin (double __x) // returns sine of x
double sqrt (double __x) // returns square root of x
double tan (double __x) // returns tangent of x
double exp (double __x) // function returns the exponential value of x.
double atan (double __x) // arc tangent of x
double atan2 (double __y, double __x) // arc tangent of y/x
double log (double __x) // natural logarithm of x
double log10 (double __x) // logarithm of x to base 10.
double pow (double __x, double __y) // x to power of y
double square (double __x) // square of x

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:02 pm

AUDRINO boards

DUO : has 54 pins and 12 PWM pins, operates at 3.3V

ANALOGWRITE :

analogwrite();//PWM this example dims the led according to the potentiometer:

Code:
int ledPin = 9;// digital pin connected to a led
int analogPin = 3;// potentiometer connected to analog pin 3
void setup(){
  pinMode(ledPin,OUTPUT);//sets pin as output
}
void loop(){
  val = analogRead(analogPin);//read the input pin
  analogWrite(ledPin,(val/4));//analogRead goes up to 1023 while analog write goes to 255
}

RANDOM NUMBERS

Code:
void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));//assuming pin 0 is unconnected so the white noise will make random more random
}
void loop(){
  Serial.print("random1= ");randNumber=random(300);//generates 0 to 299 rnd num
  Serial.print(randNumber);
  randNumber=random(10,20);//10 to 19
  Serial.print("random2= ");Serial.print(randomNumber);
}

INTERRUPTS

Code:
int pin = 2;//define interrupt pin
volatile int state = LOW;// this global async var will be used
// by the main loop and the interrupt (ISR) function
void setup(){
  pinMode(13,OUTPUT);//set pin 13 as output
  attachInterrupt(digitalPinToInterrupt(13),blink,CHANGE);
// interrupt main and run the blink function when pin 2 changes value
}
void loop(){
  digitalWrite(13,state);
}
void blink(){
//this is the ISR function
  state = !state;//toggle the state when interrupt occurs
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:05 pm

Arduino Grimoire Blink

Code:
/*
  This program blinks pin 13 of the Arduino (the
  built-in LED)
*/

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  // turn the LED on (HIGH is the voltage level)
  digitalWrite(13, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  // turn the LED off by making the voltage LOW
  digitalWrite(13, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:07 pm

FADE LED

Arduino Grimoire Fade

Code:
/*
  Fade
  This example shows how to fade an LED on pin 9
  using the analogWrite() function.

  The analogWrite() function uses PWM, so if  you
  want to change the pin you're using, be  sure to
  use another PWM capable pin. On most  Arduino,
  the PWM pins are identified with  a "~" sign,
  like ~3, ~5, ~6, ~9, ~10 and ~11.
*/

int brightness = 0;
int fade = 5;
void setup()
{
  pinMode(9, OUTPUT);
}

void loop()
{
  analogWrite(9,brightness);
  brightness+=fade;
  if(brightness==0||brightness==225){fade=-1 * fade;  }
  delay(200);
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:09 pm

ANALOG READ
alert note : Arduino code is C++

Arduino Grimoire Analog-Read

Code:
/*
    Analog Input  Demonstrates analog input by
  reading an analog sensor on analog pin 0 and
  turning on and off a light emitting diode(LED)
  connected to digital pin 13.  The amount of time
  the LED will be on and off depends on  the value
  obtained by analogRead().

  The circuit:
  * Potentiometer attached to analog input 0
  * center pin of the potentiometer to the analog
  pin
  * one side pin (either one) to ground
  * the other side pin to +5V
  * LED anode (long leg) attached to digital
  output 13
  * LED cathode (short leg) attached to ground
  * Note: because most Arduinos have a built-in
  LED attached  to pin 13 on the board, the LED is
  optional.

  Created by David Cuartielles
  modified 30 Aug 2011  By Tom Igoe

  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/AnalogInput
*/

int sensorValue = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  // read the value from the sensor
  sensorValue = analogRead(A0);
  // turn the LED on
  digitalWrite(13, HIGH);
  // stop the program for the <sensorValue>
  // milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
    digitalWrite(13, LOW);
  // stop the program for the <sensorValue>
  // milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:11 pm

alert note :
you must use Serial.begin(9600); in the setup function in order to use Serial.print();
and see the messages it prints in the serial monitor

LED GRAPHBAR DISPLAY

Arduino Grimoire Graph-bar-display

Code:
/*
  AnalogReadSerial
  Reads an analog input (potentiometer) on pin 0,
  prints the result to the serial monitor.

  OPEN THE SERIAL MONITOR TO VIEW THE OUTPUT FROM
  THE POTENTIOMETER >>

  Attach the center pin of a potentiometer to pin
  A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

int sensorValue = 0;
const int ledCount=4;
int ledPins[] = {6,7,8,9};

void setup()
{
  pinMode(A0, INPUT);
  for(int thisLed = 0;thisLed<ledCount;thisLed++){
  pinMode(thisLed,OUTPUT);
  }
  Serial.begin(9600);

}

void loop()
{
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  int ledLevel = map(sensorValue,0,1023,0,ledCount);
  for(int thisLed = 0;thisLed<ledCount;thisLed++){
    if(thisLed<ledLevel){digitalWrite(thisLed+6,HIGH);}
    else{digitalWrite(thisLed+6,LOW);}
  }
  delay(10); // Delay a little bit to improve simulation performance
}

this will display the rheostat current level using the map function to convert the sensor value which ranges 0->1023 to the led amount

For the mathematically inclined, here’s the whole function

Code:
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:15 pm

simple button hello world

alert note pin 13 is a built in led

Arduino Grimoire Button

Code:
/*
  Button
  Turns on and off a light emitting diode(LED)
  connected to digital  pin 13, when pressing a
  pushbutton attached to pin 2.

  The circuit:
  * LED attached from pin 13 to ground
  * pushbutton attached to pin 2 from +5V
  * 10K resistor attached to pin 2 from ground
  * Note: on most Arduinos there is already an LED
  on the board  attached to pin 13.

  created 2005  by DojoDave <http://www.0j0.org>

  modified 30 Aug 2011  by Tom Igoe

  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/Button
*/

int buttonState = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  // read the state of the pushbutton value
  buttonState = digitalRead(2);
  // check if pushbutton is pressed.  if it is, the
  // buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(13, HIGH);
  } else {
    // turn LED off
    digitalWrite(13, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:17 pm

KEYBOARD SEND KEYS

this code beef up makes the arduino take over the keyboard and send keys to the computer.
//add the keyboard library :
#include "Keyboard.h"

in setup() :
Keyboard.begin();

in the loop() function:
while(digitalRead(2)==HIGH){delay(500);}//trigger btn connected to pin 2
delay(1000);
//ctrl+alt+delete
KeyBoard.press(KEY_LEFT_CTRL);
KeyBoard.press(KEY_LEFT_ALT);
KeyBoard.press(KEY_DELETE);
delay(100);Keyboard.releaseAll();
while(true);// fail safe anti glitch

###################################

SEND KEYBOARD MESSAGE

globaly :
//add the keyboard library :
#include "Keyboard.h"
in setup(): Keyboard.begin();
in loop() :
//inside a trigger such as btn press :
Keyboard.print("hello world");

###################################

MOUSE library :
uses an arduino Leonardo, Micro, or Due

it works like the keyboard one :
global :
#include "Mouse.h"
setup() func:
Mouse.begin();
void func some on-trigger commands available examples:

Mouse.move(x,y,0); Mouse.isPressed(MOUSE_LEFT);Mouse.press(MOUSE_LEFT);

###################################

ADDING A LIBRARY :
SKETCH, INCLUDE LIBRARY. ADD the ready library or a zip library you DLed (from github),
in the code declare: #include "library_name.h"

this will load examples to the toolbar menu of the arduino IDE as well

sketch, library manager is a 2nd way :
type library name, install
the libraries added will be at documents, Arduino, libraries and can be deleted.
so you can also just paste the library files to that dir

note : make sure the lib files are the same in the directory or rename them if library error shows

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:18 pm

READING KEYBOARD (keyloager)

uses arduino micro Duo or leonardo

add the keypad library. to test this code you can use the notpad

Code:
#include "Keyboard.h"
void setup(){
  //open the serial port
  Serial.begin(9600);
  Keyboard.begin();// init control over the keyboard
}
void loop(){
  //check for serial data (keyboard input)
  if(Serial.available()>0){char inChar = Serial.read();Keyboard.write(inChar+1);}//ceaser coding
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:19 pm

SENSORS

HUMIDITY SENSOR (DHT)

data pin : connect to pin 2
Vccpin to 5V of arduino board
ground to arduino ground
10Kohm resistor (pull up resistor) between Vcc and data pin

Code:
#include "DHT.h"
#define DHTPIN 2 // what digital pin we are connected to
#define DHTTYPE DHT22 //depends on the sensor model
DHT dht(DHTPIN, DHTTYPE);

void setup(){
  Serial.begin(9600);
  Serial.println(DHT test start");
  dht.begin();
}
void loop(){
  delay(2000);//wait between mesurments
  float h = dht.readHumidity();
  //check on failed mesurments and if so exit
  if(isnan(h)){Serial.println("failed to read from DHT sensor tbh ngl");
  return;}
  //compute to celsius
  Serial.print("humidity :");
  Serial.print(h);Serial.println(" %\t")
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:19 pm

ARDUINO WATER DETECTOR

connect led to pin 9
connect water sensor to ground, 5V and its sensor pin to sensor number 8.

Code:
#define Grove_WaterSensor 8
#define led 9
void setup(){
  pinMode(Grove_WaterSensor,INPUT);
  pinMode(LED,OUTPUT);
}
void loop(){
  if(digitalRead(Grove_WaterSensor)==LOW){digitalWrite(LED, HIGH);}
  else{digitalWrite(LED, LOW);}
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:20 pm

ULTRASONIC SENSOR
this sensor gets distances. so a bot can use it to avoid falling from high places.

connect :
5v pin, ground pin, trigger to digital pin 7, echo pin to pin 6

Code:
const int pingPin = 7;// Trigger pin of Ultrasonic sensor
const int echoPin = 6;//Echo pin of Ultrasonic sensor

void setup(){
  Serial.begin(9600);// starting serial terminal
}
void loop(){
  long cm = getDistance();
  Serial.print(cm);Serial.println(" centimeters");delay(100);
}
long getDistance(){
 long duration, inches, cm;
  pinMode(pingPin,OUTPUT);
  digitalWrite(pingPin,LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin,LOW);
  pinMode(echoPin,INPUT);
  duration = pulseIn(echoPin,HIGH);
  return microSecondsToCentimeters(duration);
}
long microSecondsToCentimeters(long microSeconds){return microSeconds/29/2;}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:21 pm

DC MOTOR CONTROL WITH H GATE :



link project

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:23 pm

SERVO MOTOR

Arduino Grimoire Servo

1 Audrino UNO board
1 servo motor
1 ULN2003 driving IC
1 10k ohm rheostat

alert the IC connects to the servo ground instead of the Audrino to prevent damage from sudden voltage drops

Code:
/* Controlling a servo position using a potentiometer (variable resistor) */
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
  myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
  val = analogRead(potpin);  // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);  // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val); // sets the servo position according to the scaled value
  delay(15);
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:24 pm

STEPPER MOTOR

Arduino Grimoire Stepper

1 Audrino ONO board
1 small bipolar stepper motor
1 LM298 driving IC

Code:
/* Stepper Motor Control */
#include <Stepper.h>
const int stepsPerRevolution = 90;// change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {  // set the speed at 60 rpm:
  myStepper.setSpeed(5);  // initialize the serial port:
  Serial.begin(9600);}
void loop() {  // step one revolution in one direction:
  Serial.println("clockwise"); 
  myStepper.step(stepsPerRevolution);
  delay(500);  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:27 pm

ARDUINO - WIRELESS COMMUNICATION

Arduino Grimoire Transmitter

Arduino Grimoire Receiver

receiver module : MX-05V
transmitter module : MX-FS-03V

Arduino Code for Transmitter

Code:
//simple Tx on pin D12
#include <VirtualWire.h>
char *controller;
 
void setup() {
   pinMode(13,OUTPUT);
   vw_set_ptt_inverted(true);
   vw_set_tx_pin(12);
   vw_setup(4000);// speed of data transfer Kbps
}
 
void loop() {
   controller="1" ;
   vw_send((uint8_t *)controller, strlen(controller));
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13,1);
   delay(2000);
   controller="0" ;
   vw_send((uint8_t *)controller, strlen(controller));
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(13,0);
   delay(2000);
}

Code to Note
This is a simple code. First, it will send character '1' and after two seconds it will send character '0' and so on.

Arduino Code for Receiver

Code:
//simple Rx on pin D12
#include <VirtualWire.h>
 
void setup() {
   vw_set_ptt_inverted(true); // Required for DR3100
   vw_set_rx_pin(12);
   vw_setup(4000); // Bits per sec
   pinMode(5, OUTPUT);
   vw_rx_start(); // Start the receiver PLL running
}
 
void loop() {
   uint8_t buf[VW_MAX_MESSAGE_LEN];
   uint8_t buflen = VW_MAX_MESSAGE_LEN;
   if (vw_get_message(buf, &buflen)) // Non-blocking {
      if(buf[0]=='1') {
         digitalWrite(5,1);
      }
      if(buf[0]=='0') {
         digitalWrite(5,0);
      }
   }
}

Code to Note
The LED connected to pin number 5 on the Arduino board is turned ON when character '1' is received and turned OFF when character '0' received.

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:31 pm

Arduino Wireless Communication – NRF24L01​



Arduino Grimoire Transiver

download and install the RF24 library : https://github.com/nRF24/RF24

Transmitter Code :

Code:
/*
* Arduino Wireless Communication Tutorial
*     Example 1 - Transmitter Code
*              
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  delay(1000);
}

Receiver Code

Code:
/*
* Arduino Wireless Communication Tutorial
*       Example 1 - Receiver Code
*              
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

Troubleshooting​
It’s worth noting that power supply noise is one of the most common issues people experience when trying to make successful communication with the NRF24L01 modules. Generally, RF circuits or radio frequency signals are sensitive to power supply noise. Therefore, it’s always a good idea to include a decoupling capacitor across the power supply line. The capacitor can be anything from 10uF to 100uF.

Arduino Grimoire Transiver-antiglitch

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:34 pm

SOLENOID

Arduino Grimoire Solenoid

Controlling a Solenoid with an Arduino​

a solenoid is an actuator or electric water valve

witch a small 5v Solenoid on and off in intervals of 1 second, similar to the LED Blink we created earlier on. You will need the following components for this project:

1 x Arduino Uno
1 x Solderless breadboard
5 x Jumper Wires
1 x 220 Ω Resistor
1 x Diode
1 x Power Transistor
1 x 5v Solenoid
Connect 5v Power and Ground from your Arduino to your power and ground rails on your breadboard
Connect your solenoid to separate lines on your breadboard, one to the 5v power from step 2, the other needs to connect to the collector (middle) of the transistor.
Connect your Diode between the two solenoid cables, this will prevent current discharging back through the circuit when the solenoid coil discharges.
Insert your power transistor on three separate lines of your breadboard, with the flat side facing toward the outside. Ensure the collector's leg is connected to the solenoid and diode line.
Connect a 220-ohm Resistor from the base leg of the transistor to a separate line
Connect the emitter leg to the ground rail.
Connect the other side of the resistor from step 6 to digital pin 9, that's our control pin.

Code:
int solenoidPin = 9;                    //This is the output pin on the Arduino

void setup()
{
  pinMode(solenoidPin, OUTPUT);          //Sets that pin as an output
}

void loop()
{
  digitalWrite(solenoidPin, HIGH);      //Switch Solenoid ON
  delay(1000);                          //Wait 1 Second
  digitalWrite(solenoidPin, LOW);      //Switch Solenoid OFF
  delay(1000);                          //Wait 1 Second
}

you can replace the 5v with an external 12V power source or use the arduino 12v vcc for 12V solenoids

similar project :


_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:37 pm

PULLUP RESISTOR

Arduino Grimoire PULLUP-resistor

PULLUP resistor :
when a reader pin is not connected to anything it will read white noise which is the left over spirit of dead people
making it impossible to read if the button connected to it is closed or opened. the solution is to connect 5v->10K ohm resistor between the BTN
and the pin wire, which pull up the read out to high when the button is open.

Code:
int pushButton = 2;
void setup() {
  //pinMode(pushButton, INPUT_PULLUP);
  pinMode(pushButton, INPUT);
  pinMode(13,OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  if(buttonState==LOW){digitalWrite(13,HIGH);}
  else{digitalWrite(13,LOW);}
  delay(1);
}

a cleaner solution is to comment out : pinMode(pushButton, INPUT);
and comment in pinMode(pushButton, INPUT_PULLUP); then you don't need the 10K ohm resistor or the red connection wire at all
(because you are using a built in pull up resistor)

at any rate when you click the btn you will see the built in LED 13 light up

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:41 pm

SHIFT REGISTERS
this convert 1 output pin into 8 (you send bytes)

Arduino Grimoire Shift-register

Code:
//Pin connected to latch pin (ST_CP) of 74HC595
int latchPin = 8; //Pin connected to clock pin (SH_CP) of 74HC595
int clockPin = 12; //Pin connected to Data in (DS) of 74HC595
int dataPin = 11;
void setup() {
//set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, OUTPUT);
 pinMode(clockPin, OUTPUT); }
void loop() {
// loop through 0 to 256
int i; for(i=0; i<256; i++) {
// turn off the output so the pins don’t light up
// while you’re shifting bits:
 digitalWrite(latchPin, LOW);
 shiftOut(dataPin, clockPin, LSBFIRST, i);
// turn on the output so the LEDs can light up:
 digitalWrite(latchPin, HIGH);
 delay(300);
 }
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:45 pm

CONNECTING 2 SHIFT REGISTERS

Arduino Grimoire 2-shift-registers

Code:
//Pin connected to latch pin (ST_CP) of 74HC595
int latchPin = 8;
 //Pin connected to clock pin (SH_CP) of 74HC595
int clockPin = 12;
 //Pin connected to Data in (DS) of 74HC595
int dataPin = 11;
 // number of shift registers used
int numRegisters = 3;
 // first pattern to be displayed
int pattern1 = 85;
 // second pattern to be displayed
int pattern2 = 170;
void setup() {
 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT);
 // start with all LEDs off
setLEDs(0); }
void loop() {
 // turn on LEDs in the pattern 01010101
setLEDs(pattern1);
 // wait 1 sec
 delay(1000);
 // turn on LEDs in the pattern 10101010
 setLEDs(pattern2);
 // wait 1 sec
delay(1000); }
 // sends pattern to shift register for // which LEDs to turn on and off
void setLEDs(int lightPattern) {
 // turn off the output so the pins don’t light up
 // while you’re shifting bits:
 digitalWrite(latchPin, LOW);
 int i; for(i=0; i<numRegisters; i++) {
 // sends out the pattern once for each shift register
 shiftOut(dataPin, clockPin, LSBFIRST, lightPattern); }
 // turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
 delay(300);
}

_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:47 pm

capacitive touch sensor switch out of anything metal using an Arduino​

Arduino Grimoire Capacitive-touch-sensor

Code:
#include <CapacitiveSensor.h>
CapacitiveSensor Sensor = CapacitiveSensor(4, 6);
long val;
int pos;
#define led 13

void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop()
{

val = Sensor.capacitiveSensor(30);
Serial.println(val);
if (val >= 1000 && pos == 0)
{
digitalWrite(led, HIGH);
pos = 1;
delay(500);
}

else if (val >= 1000 && pos == 1)
{
digitalWrite(led, LOW);
pos = 0;
delay(500);
}

delay(10);
}


_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:47 pm

fritzing, PCB boards and arduino shields


_________________
MB over and out emp it up
Back to top Go down
Moti Barski
super
super
Moti Barski

Posts : 547
Join date : 2011-08-02

Arduino Grimoire Empty
PostSubject: Re: Arduino Grimoire   Arduino Grimoire EmptySun Apr 18, 2021 9:50 pm

9v battery to DC barrel connector is used to power arduinos without a computer

Arduino Grimoire 9v-barrel-connector

_________________
MB over and out emp it up
Back to top Go down
 
Arduino Grimoire
Back to top 
Page 1 of 2Go to page : 1, 2  Next

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: