Skip to content

Instantly share code, notes, and snippets.

@Eyakub
Last active July 17, 2018 06:57
Show Gist options
  • Save Eyakub/78c1eae3b9b0fcb3224ae2129024a5d8 to your computer and use it in GitHub Desktop.
Save Eyakub/78c1eae3b9b0fcb3224ae2129024a5d8 to your computer and use it in GitHub Desktop.
Here I'll be adding some basic adruino basic code.
int state = 0;
int fading = 15;
int brightness = 0;
int ledOut = 5;
int val;
int value;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(ledOut, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
val = Serial.read();
val=Serial.parseInt();
Serial.println(val);
//value = val;
//value = Serial.parseInt();
if(val == 1){
digitalWrite(ledOut, HIGH);
delay(200);
digitalWrite(ledOut, LOW);
delay(200);
}
else{
analogWrite(ledOut, brightness);
brightness = brightness + fading;
if(brightness <= 0 || brightness >= 255){
fading = -fading;
}
delay(30);
}
}
int ledPin = 5;
int inPin = 13;
int val = 0;
int brightness = 0;
int fadeAmount = 5;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(inPin);
if(val == HIGH)
{
Serial.print("Blinking");
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
else{
Serial.print("Fading");
// set the brightness of pin 5:
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
void setup() {
Serial.begin(9600);
pinMode(8,OUTPUT);
pinMode(A0,INPUT);
// put your setup code here, to run once:
}
void loop() {
int value=analogRead(A0);
Serial.println(value);
if(value>100){
digitalWrite(8,LOW);
}else{
digitalWrite(8,HIGH);
}
// put your main code here, to run repeatedly:
}
int ledPin = 13; // LED connected to digital pin 13
int fadePin = 3;
unsigned long nextBlink, nextFade;
int fadeBrightness = 0;
char ledState = LOW, fadeDir = 10;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(fadePin, OUTPUT);
ledState = LOW;
nextBlink = millis();
nextFade = millis();
}
/*
* This checks whether it's time to change the state of the LED, and
* does the write if so. But it doesn't execute delay(), so in either
* case it takes essentially no time to execute.
*/
void blink(void)
{
unsigned long now;
now = millis();
if (now >= nextBlink) {
nextBlink = now + 1000; // Next change in one second
if (ledState == LOW) {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
}
else {
digitalWrite(ledPin, LOW);
ledState = LOW;
}
}
}
void fade(void)
{
unsigned long now;
now = millis();
if (now >= nextFade) {
nextFade = now + 200; // Next change in one second
if (fadeDir > 0) {
if (fadeBrightness > (255 - fadeDir)) {
fadeDir = - fadeDir;
}
}
else {
if (fadeBrightness < (-fadeDir)) {
fadeDir = - fadeDir;
}
}
fadeBrightness += fadeDir;
analogWrite(fadePin, fadeBrightness);
}
}
void loop()
{
blink();
fade();
}
int val = 0; // variable for reading the pin status
void setup() {
pinMode(8, OUTPUT); // declare LED as output
pinMode(2, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(2); // read input value
if(val == 1){
//pirState = HIGH;
//Serial.print("Motion detected");
digitalWrite(8, HIGH);
}
else{
pirState = LOW;
digitalWrite(8, LOW);
}
}
#include<LiquidCrystal.h>
const int rs = 10, en = 12, rw = 11, d0 = 2, d1 = 3, d2 = 4, d3 = 5;
LiquidCrystal lcd(rs, en, rw, d0, d1, d2, d3);
int backLight = 13;
void setup() {
// put your setup code here, to run once:
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hello ");
lcd.setCursor(0, 1);
lcd.print("Wrold");
}
void loop() {
// put your main code here, to run repeatedly:
}
int tempPin = A0; // the output pin of LM35
//int fan1 = 4; // the pin where fan is
//int fan2 = 8; // led pin
int temp;
int tempMin = 20; // the temperature to start the fan
int tempMax = 50; // the maximum temperature when fan is at 100%
//int fanSpeed;
void setup() {
pinMode(fan1, OUTPUT);
pinMode(fan2, OUTPUT);
pinMode(tempPin, INPUT);
analogReference(INTERNAL);
Serial.begin(9600);
}
void loop() {
temp = readTemp(); // get the temperature
if(temp < tempMin) { // if temp is lower than minimum temp
//fanSpeed = 0; // fan is not spinning
digitalWrite(8, LOW);
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, LOW);
digitalWrite(8, HIGH);
}
Serial.print("TEMP = ");
Serial.print(temp);
Serial.println("DEG C");
}
int readTemp() { // get the temperature and convert it to celsius
temp = analogRead(tempPin);
return temp * 0.4888;
}
int trigPin = 9;
int echoPin = 10;
long duration;
int distance;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//sets the trigPin on high state for 10micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
//reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//calculating the distance onthe serial monitor
distance = duration * 0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment