arduino code for programming a analoge pot to control 2 leds,1 to come on at 1.5V and the other other at 3.5v first time using this any help will do thanks
This should work:
#define LED1 4
#define LED2 5
#define POT A0
void setup()
{
pinMode(POT, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
int val = 0;
void loop()
{
val = analagRead(POT);
if(val > 308) // 1.5/max voltage (say 5) x resolution (1024)
digitalWrite(LED1, HIGH);
else if(val > 717) // 3.5/max voltage (say 5) x resolution (1024)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH) ;
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
}
Fix some syntax errors, I am typing from cellphone.
Related
I am doing a project using sensors (LDR & Ultrasonic) and LEDs using the Arduino Software. I have managed to get the light bulb to work, however I would like to know on how to combine two different Arduino programs into one. Attached below are the two different programs
Program 1:
int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}
void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
{
digitalWrite(3,HIGH);//Makes the LED glow in Dark.
}
else
{
digitalWrite(3,LOW);//Turns the LED OFF in Light.
}
}
Program 2:
#define trigPin 13
#define echoPin 12
#define led 11
void setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{ long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Thank you!
You can use a timer and each 500 milliseconds run loop2
The first loop run normaly but the second loop run each 500 milliseconds
#define trigPin 13
#define echoPin 12
#define led 11
#define DELAY 500
int ldr = A0; //Set A0(Analog Input) for LDR.
int value = 0;
long timer = millis(); // a timer for timing 500 milliseconds
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
value = analogRead(ldr); //Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if (value < 300)
{
digitalWrite(3, HIGH); //Makes the LED glow in Dark.
}
else
{
digitalWrite(3, LOW); //Turns the LED OFF in Light.
}
if (millis() - timer >= DELAY ) {
timer = millis();//reset timer
loop2();
}
}
void loop2() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{ digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
Serial.print(distance);
Serial.println(" cm");
}
So, I know it's probably SUPER simple, but I'm new to arduino and I'm just drawing a blank. I'm making a motion detector with an HC-SRO4 ultrasonic sensor. Right now I have it set so whenever it senses an object within 60 cm, it turns on an LED light, but when the object disappears, the LED turns off. What I would like to happen is that it would stay on until I press a button to reset it. Any help is really appreciated and i thank you in advance.
void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1
;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}
for this you have make low pushbutton pin while pressing.once the oobject came near the flag will set once the set it will move to while loop and run until the button pressed
#define LED 8
#define trigPin 12
#define echoPin 13
#define push_button 5
int flag=0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
pinMode(push_button,INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(digitalRead()==0)
{
flag=0;
}
if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
flag=1;
while(flag=1)
{
digitalWrite(LED, HIGH);}
}}
It this a really simple code about turning on an off some LEDs, but I want it not to be so repetititve
I've tried to make a loop, but i couldn't get it, I did my best, but I'm really bad at this :(( pls need someone's jelp
`
#define LED 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
void setup()
{
pinMode(LED, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED3, HIGH);
delay(250);
digitalWrite(LED4, HIGH);
delay(125);
digitalWrite(LED5, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(1000);
digitalWrite(LED2, LOW);
delay(500);
digitalWrite(LED3, LOW);
delay(250);
digitalWrite(LED4, LOW);
delay(125);
digitalWrite(LED5, LOW);
delay(500);
}`
I'll rewrite what Oleg Mazurov said in the comment:
#define NUMBER_OF_LEDS 5
static const uint8_t a_led[NUMBER_OF_LEDS] = {2, 3, 4, 5, 6};
static const uint16_t a_delay[NUMBER_OF_LEDS] = {1000, 500, 250, 125, 500};
void setup() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
pinMode(a_led[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
digitalWrite(a_led[i], !digitalRead(a_led[i]));
delay(a_delay[i]);
}
}
I have a game that consist of 4 direction of movement (up down left and right)
using Arduino Nano and analog joystick, seems like code is right as check before other posts.
This is the Arduino code:
byte x_axis = A3;
byte y_axis = A1;
byte btn1 = 8;
byte btn2 = 9;
byte btn3 = 10;
byte btn4 = 11;
byte btn5 = 12;
byte led = 13;
void setup(){
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(btn4, INPUT);
pinMode(btn5, INPUT);
digitalWrite(btn1, HIGH);
digitalWrite(btn2, HIGH);
digitalWrite(btn3, HIGH);
digitalWrite(btn4, HIGH);
digitalWrite(btn5, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop(){
Int read_x = analogRead(x_axis);
int read_y = analogRead(y_axis);
if(read_x > 600){
Serial.println("R");
digitalWrite(led, HIGH);
}
if(read_x < 400){
Serial.println("L");
digitalWrite(led, HIGH);
}
if(read_y > 600){
Serial.println("D");
digitalWrite(led, HIGH);
}
if(read_y < 400){
Serial.println("U");
digitalWrite(led, HIGH);
}
if(digitalRead(btn1) == LOW){
Serial.println("1");
digitalWrite(led, HIGH);
}
if(digitalRead(btn2) == LOW){
Serial.println("2");
digitalWrite(led, HIGH);
}
if(digitalRead(btn3) == LOW){
Serial.println("3");
digitalWrite(led, HIGH);
}
if(digitalRead(btn4) == LOW){
Serial.println("4");
digitalWrite(led, HIGH);
}
if(digitalRead(btn5) == LOW){
Serial.println("5");
digitalWrite(led, HIGH);
}
delay(10);
digitalWrite(led, LOW);
}
But when I use serial monitor to check it, it non stop show me U and L even without touching the joystick.
How can I fix this problem?
I'm feeling generous, so you have some jitter issues in the code and you really could use some cleanup. This code compiles. Ok now when a joystick is 0,0 in x/y physically it jitters in code. One thing you could do is remap out the jitter to give a wider center. print the raw analog values to the serial monitor and then map them out to your 0 point with a little padding. reference: https://www.arduino.cc/reference/en/language/functions/math/map/
byte x_axis = A3;
byte y_axis = A1;
byte btn[] = {8, 9, 10, 11, 12}; // 2,3,4,5,6
byte stat[] = {1, 2, 3, 4, 5};
byte led = 13;
int dval = 50;
void setup() {
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
for (int i = 0; i < 5; i++) {
pinMode(btn[i], INPUT);
digitalWrite(btn[i], HIGH);
}
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop() {
int read_x = analogRead(x_axis);
delay(dval);
int read_y = analogRead(y_axis);
delay(dval);
if (read_x > 600) {
Serial.println("R");
digitalWrite(led, HIGH);
}
if (read_x < 400) {
Serial.println("L");
digitalWrite(led, HIGH);
}
if (read_y > 600) {
Serial.println("D");
digitalWrite(led, HIGH);
}
if (read_y < 400) {
Serial.println("U");
digitalWrite(led, HIGH);
}
for (int i = 0; i < 5; i++) {
if (digitalRead(btn[i]) == LOW) {
Serial.println(stat[i]);
digitalWrite(led, HIGH);
}
}
delay(10);
digitalWrite(led, LOW);
}
Emad joon:
1- Check that the ground of your joy stick and the Vdd is connected to your arduino.
2- connect the x and y of the joy stick to the arduino analog inputs.
use this code as starter:
#define X_AXIS A1
#define Y_AXIS A3
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("X = ");
Serial.println(analogRead(X_AXIS);
Serial.print("Y = ");
Serial.println(analogRead(Y_AXIS);
delay(150);
}
See what values you get when your Joy Stick is at initial position. You can build up on this code for buttons.
Also, do not forget to use debouncing for your button readings:
if(digitalRead(Button1)==0){
delay(40);
if(digitalRead(Button1==0){
buttonPressed=true;
}
}
I am trying to run a for loop inside an if statement but it keeps repeating. I basically want pin 4 to blink 6 times when I press a button on pin 2. When z becomes a 6 in the for loop, the if statement makes it so that z is reset to 0 and the for loop restart all over again. Therefore the LED on pin 4 keeps blinking on and on. Anyone can help so that it blinks only 6 times? Here is the code:
int switchState = 0;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
switchState = digitalRead(2);
if (switchState == LOW)
{
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
else
{
digitalWrite(5, HIGH);
for (int z=0; z<6; z++)
{
digitalWrite(4, HIGH);
delay(100);
digitalWrite(4, LOW);
delay(100);
}
}
}
Try out this:
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
//Deactivate all leds
digitalWrite(5, LOW);
digitalWrite(4, LOW);
if (digitalRead(2) == HIGH) { //If the button is pressed...
digitalWrite(5, HIGH);
//blink
for (int z=0; z<6; z++)
{
digitalWrite(4, HIGH);
delay(100);
digitalWrite(4, LOW);
delay(100);
}
while (digitalRead(2) == HIGH) { // Wait until release the button
delay(10);
}
}
}