13-02-2022, 22:37
(Ten post był ostatnio modyfikowany: 13-02-2022, 22:38 przez brzytwiarz.)
Mam problem z enkoderem tzn nie umiem odpalić go w przerwaniach a jedynie cyklicznie w pętli. Płytka na której to chodzi to mega_256 i to też tak jakoś dziwnie bo pracuje poprawnie jak kręcę wolno to działa licząc w lewo jak i w prawo ale najdziwniejsza sprawa że jak nie używam to w jakiś dziwny sposób zlicza w lewo (nikt niekręci pokrętłem a sam liczy).
Enkoder podłączony jest jak na linku.
https://zapodaj.net/1dd055f892110.png.html
Nie jestem pewien czy Piny 2,3 można używać do przerwania (Interrupt) ale wedle strony https://www.circuitstoday.com/arduino-me...schematics
[color=var(--h-color, #161616)]Hardware Interrupt Pins :[/color]
Digital pin 18 – 21,2,3 hardware interrupt is used for interrupt services. Hardware interrupt must be enabled with global interrupt enable to get interrupt from other devices.
Enkoder podłączony jest jak na linku.
https://zapodaj.net/1dd055f892110.png.html
Nie jestem pewien czy Piny 2,3 można używać do przerwania (Interrupt) ale wedle strony https://www.circuitstoday.com/arduino-me...schematics
[color=var(--h-color, #161616)]Hardware Interrupt Pins :[/color]
Digital pin 18 – 21,2,3 hardware interrupt is used for interrupt services. Hardware interrupt must be enabled with global interrupt enable to get interrupt from other devices.
Kod:
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// przyciski
// constants won't change. They're used here to set pin numbers:
const int Button_4 = 29; // the number of the pushbutton pin
const int Button_3 = 30; // the number of the pushbutton pin
const int Button_2 = 31; // the number of the pushbutton pin
const int Button_1 = 32; // the number of the pushbutton pin
const int swiatlo = 12;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState_P1 = 1;
int buttonState_P2 = 0;
int buttonState_P3 = 0;
int buttonState_P4 = 0;
int Outputs_swiatlo = 0;
// enkoder
const int encoderPinA = 3;
const int encoderPinB = 2;
int angle = 0;
int encoderPos = 0;
int encoderStepsPerRevolution = 23;
boolean encoderALast = LOW; //remembers the previous state of the encoder pin A
// PWM FAN 1
//const int FAN1_SPEED_SETTING = 5; // the number of the setting speed by PWM to pin 5 blue wire
//const int FAN1_SPEED_CONTROL = 6; // the number of the control speed from FAN1 pin 6 yelow wire
//int count_1_pulse = 0;
//unsigned long start_time_1 = 0;
//int rpm_fan_1;
//int count = 0; //test
void setup()
{
// FAN 1 speed
//pinMode(FAN1_SPEED_SETTING, OUTPUT);
//analogWrite(FAN1_SPEED_SETTING, 0);
//attachInterrupt(digitalPinToInterrupt(6),counter, RISING);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("Hello, world!");
lcd.setCursor(4, 1);
lcd.print("TiTa DUO!");
// przyciski
// initialize the pushbutton pin as an input:
pinMode(Button_4, INPUT_PULLUP);
pinMode(Button_3, INPUT_PULLUP);
pinMode(Button_2, INPUT_PULLUP);
pinMode(Button_1, INPUT_PULLUP);
pinMode(swiatlo, OUTPUT);
Wire.begin();
Serial.println("pierwsze wczytanie ustawien");
// enkoder
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
Serial.begin(9600);
}
void loop()
{
//for (int pwm = 0; pwm <=255; pwm += 51){
// analogWrite(FAN1_SPEED_SETTING, pwm);
// delay(5000);
// start_time_1 = millis();
// count = 0;
// while ((millis() - start_time_1) < 1000){
// }
// rpm_fan_1 = count * 30; //60/2
// Serial.print("PWM = ");
// Serial.print(map(pwm, 0, 255, 0, 100));
// Serial.print("% , Speed = ");
// Serial.print(rpm_fan_1);
// Serial.print(" rpm");
// }
//}
if (buttonState_P1 == LOW) {
lcd.clear();
lcd.backlight();
} //
lcd.setCursor(2, 0);
lcd.print(" ");
lcd.print(encoderPos);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.print(angle);
lcd.print(" ");
// read the state of the pushbutton value:
buttonState_P4 = digitalRead(Button_4);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState_P4 == HIGH) {
// turn LED on:
Serial.println("nie_wcisniety p4");
digitalWrite(swiatlo, LOW);
} else {
// turn LED off:
Serial.println("przycisnieto p4");
digitalWrite(swiatlo, HIGH);
}
// read the state of the pushbutton value:
buttonState_P3 = digitalRead(Button_3);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState_P3 == HIGH) {
// turn LED on:
// Serial.println("nie_wcisniety button 3");
} else {
// turn LED off:
// Serial.println("przycisnieto p3");
lcd.noBacklight(); // wyłączenie podswietlenia LCD
}
// read the state of the pushbutton value:
buttonState_P2 = digitalRead(Button_2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState_P2 == HIGH) {
// turn LED on:
// Serial.println("nie_wcisniety p2");
} else {
// turn LED off:
// Serial.println("przycisnieto p2");
}
// read the state of the pushbutton value:
buttonState_P1 = digitalRead(Button_1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState_P1 == HIGH) {
// turn LED on:
// Serial.println("nie_wcisniety p1");
} else {
// turn LED off:
Serial.println("przycisnieto p1");
// lcd.backlight(); // załączenie LCD
}
// enkoder
boolean encoderA = digitalRead(encoderPinA);
if ((encoderALast == HIGH) && (encoderA == LOW))
{
if (digitalRead(encoderPinB) == LOW)
{
encoderPos++;
}
else
{
encoderPos--;
}
angle = (encoderPos % encoderStepsPerRevolution) * 360 / encoderStepsPerRevolution;
Serial.print(encoderPos);
Serial.print(" ");
Serial.println(angle);
}
encoderALast = encoderA;
}
// FAN 1 RPM SPEED
//void counter (){
// count++;
//}