I'm trying to make the LED on the Wemos D1 mini R2 ESP8266 light up gradually.
I try this code:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
for (int i = 0; i < 200; i++){
analogWrite(LED_BUILTIN, i);
delay(10);
}
for (int i = 200; i > 0; i--){
analogWrite(LED_BUILTIN, i);
delay(10);
}
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
delay (2000);
}
And it does not work. I changed LED_BUILTIN to D3 and it works, but when I changed LED_BUILTIN to D4, it doesn't work.
analogWrite command is for sending pwm signal in different duty-cycle
as far as I know the built_in led is not capable of working in pwm mode just regular on-off
It looks like the LED is wired to D3.
Related
I was trying to use a Arduino Mega to substitute a Arduino Uno. The folliwing code works fine in Uno but doesn't work on Mega (I already changed the ports to it). It is used for a Capacitive touchsensor:
#define resolution 8
#define mains 60 // 60: north america, japan; 50: most other places
#define refresh 2 * 1000000 / mains
void setup() {
Serial.begin(9600);
// unused pins are fairly insignificant,
// but pulled low to reduce unknown variables
for(int i = 2; i < 14; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
for(int i =22; i < 53; i++){
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
pinMode(8, INPUT);
startTimer();
}
void loop() {
Serial.println(time(8, B00100000), DEC);
}
long time(int pin, byte mask) {
unsigned long count = 0, total = 0;
while(checkTimer() < refresh) {
// pinMode is about 6 times slower than assigning
// DDRB directly, but that pause is important
pinMode(pin, OUTPUT);
PORTH = 0;
pinMode(pin, INPUT);
while((PINH & mask) == 0)
count++;
total++;
}
startTimer();
return (count << resolution) / total;
}
extern volatile unsigned long timer0_overflow_count;
void startTimer() {
timer0_overflow_count = 0;
TCNT0 = 0;
}
unsigned long checkTimer() {
return ((timer0_overflow_count << 8) + TCNT0) << 2;
}
I would like to know what I need to change on the Timer interrupt stuff to make it work properly.
I'm making a simple temperature sensor to light one of two LEDs depending on the temperature.
For some reason the LED output only blinks the onboard LED (pin 13 on the Edison) once.
My temperature output is working fine, but I'm not sure why my code is working incorrectly.
Photo of the wiring here.
int temppin = 0;
int ledhigh = 7;
int ledlow = 8;
void setup()
{
Serial.begin(9600);
pinMode(temppin, INPUT);
pinMode(ledhigh, OUTPUT);
pinMode(ledlow, OUTPUT);
}
void loop()
{
int tempout = analogRead(temppin);
float volts = tempout * 5.0;
volts /= 1024.0;
float temp = (volts - 0.5) * 100 ;
Serial.print(temp); Serial.println(" celsius");
if (temp > 0){
Serial.print("high temp =");
digitalWrite(ledhigh, HIGH);
} else {digitalWrite(ledlow, HIGH);
Serial.print("low temp");
}
delay(3000);
}
The problem is probably that you're trying to use the analog input pins as output. You need to use the digital pins.
As explained in this video:
https://youtu.be/BtLwoNJ6klE?t=50s
I'm trying to make a microcontroller with an arduino. I am supplying with +5volt from the arduino, sending it to an NC button (so i can manually decide when to output a certain timed pulse). After the button it goes to a pin that I have set as an inPin (pin8). Then I want the program to make pin 7 HIGH(with a delay), and then it goes to a transistor.
This is the code I tried making (I know almost nothing about coding):
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
if (inPin=HIGH) {
digitalWrite(ledPin, HIGH);
}
delay (500);
digitalWrite(ledPin, LOW);
}
For some reason the outPin is HIGH all the time. I remembered to hook up a resistor to GND so the digital pin would stay LOW when supposed to be LOW.
Thanks in advance!
if(inPin=HIGH) is a mistake, first of all use "==" instead of "=". ALso you need to READ input pin state: int invalue = digitalRead(inPin);
Also, all pins by default coonfigured as inputs, so you don't need use pinMode(inPin, INPUT);
After those changes your code will look like:
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(inPin)==HIGH) digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
}
I'm using Arduino Uno and want to light four LEDs synchronously using the analogwrite method, but they are illuminating sequentially instead. Here is my code:
int brightness = 0;
int fadeAmount = 5;
boolean first = true;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
if (first) {
// these should iluminate synchronously
analogWrite(2, brightness);
analogWrite(3, brightness);
analogWrite(4, brightness);
analogWrite(5, brightness);
} else {
// these should iluminate synchronously
analogWrite(6, brightness);
analogWrite(7, brightness);
analogWrite(8, brightness);
analogWrite(9, brightness);
}
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
if (brightness == 0) {
reverseFirst();
}
}
delay(5);
}
void reverseFirst(){
if (first) {
first = false;
} else {
first = true;
}
}
analogWrite only effectively works on pins 3,5,6,9,10,11 because they are the PWM pins. According to the analogWrite source code all other pins if used with analogWrite, will default back to digitalWrite.
So I suspect you will not be able to do proper analog control on the other pins and that might be why not all of them are switching on at the same time.
If brightness is important to you then I propose that you only use the true PWM pins. If you require more than the six pins then there are analog expander chips which you can acquire or more simpler you can have a look at something like "ShiftPWM". It is a library which uses a shift register to shift pulses to its outputs effectively allowing you to control brightness of LEDs. Keep in mind that it is no longer being maintained.
I am trying to make a alarm clock with the raspberry pi and an arduino. I have been having this problem that when i use serial communication to send a number it, the lcd doesnt print the number. I know that the arduino is getting the number, for some reason it just wont print it. It instead prints weird symbols and lines. This article shows how i use serial communication between themThis is my arduino code.
#include <LiquidCrystal.h>
const int ledPin = 13;
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(0,1);
lcd.print("crystralball");
}
void loop()
{
if (Serial.available())
{
flash(Serial.read() - '0');
}
delay(1000);
}
void flash(int n)
{
for (int i = 0; i < n; i++)
{
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.write(n);
Serial.print(n);
Serial.flush();
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
Hi try to change your code in loop like this.
for (int i = 0; i < n; i++){
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.print(String(n));
Serial.print(n);
Serial.flush();
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
You have to use the method print and passing a string.