Fast PWM and Atmega1280 - atmega

Hello I have some problems to understand how to bind a Timer to a Pin and because of that my code isn't running...
#include <avr/io.h>
void init_PWM(void)
{
TCCR0A|=(1<<WGM00)|(1<<WGM01)|(1<<COM0A1)|(1<<CS00);
//Set OC0 PIN as output. It is PB3 on ATmega16 ATmega32
DDRB|=(1<<PB7);
}
void setPWM(uint8_t duty)
{
OCR0A = duty;
}
void main (void)
{
uint8_t brightness = 0;
init_PWM();
for (brightness=0; brightness<=255; brightness++)
{
setPWM(brightness);
_delay_ms(100);
}
}
My problem is how to assign the timer to PB7 ?
My goal is blinking LED on PB7 with FastPWM Mode...
Thanks in advance

for mega1280 is CS00 in TCCR0B and not in TCCR0A
void init_PWM(void) {
TCCR0A|=(1<<WGM00)|(1<<WGM01)|(1<<COM0A1);
TCCR0B|=(1<<CS00);
DDRB|=(1<<PB7);
}

Related

How to control the display and non-display of sensor values ​in Arduino using Bluetooth?

I have a sensor that connects to the body and displays muscle signals.
In the setup guide of this sensor, it is said to upload the following code on Arduino, and when we open the Serial Monitor, the sensor values start to be displayed.
Now I want to control the display of these signals using Bluetooth.
So that when I click on the start button in my App, Serial.print() will start working. Also, when I click on the Stop button, the display of these signals and numbers will stop.
Sensor setup guide is this :
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
}
And this is how it works properly :
But when I upload a piece of code that I wrote to my Arduino, it only shows me just on value.
this is my code :
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
char Incoming_value = 0;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
}
void loop() {
Incoming_value = Serial.read(); // "1" is for Start
if (Incoming_value == '1') {
Serial.println(Incoming_value);
StartSensor();
}
}
int StartSensor() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(200);
}
also please tell me How to write StopSensor Function for Stop print Sensor Value.
Try this code first (Without Bluetooth module)
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
char Incoming_value = 0;
int state = 0;
void setup() {
Serial.begin(9600);
//BTserial.begin(9600);
}
void loop() {
Incoming_value = Serial.read(); // "1" is for Start
if (Incoming_value == '1') {
state = 1;
}
else if (Incoming_value == '0') {
state = 0;
}
if (state == 1) {
StartSensor();
} else {
Serial.println(0);
}
}
int StartSensor() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(200);
}

PIC16F628A to Arduino RF Communication Fails (433MHz)

I have pic16f628a and Arduino UNO...
I use MikroC for PIC...
I use 433 mhz transmitter and receiver.
My purpose is reading datas from Arduino UNO which I send from PIC16F628A; but I couldn't success it...
The circuit of PIC16F628A (Transmitter):
The circuit of Transmitter
I connected first pin of receiver to +5V of Arduino;
second pin of receiver to 12.pin of Arduino,
last pin of receiver to GND pin of Arduino.
Transmitter(PIC16F628A):
char pre[15]={'U','U','U','U','U',255,255,255,255,255,0,0,0,0,0}; //start bytes...
char ileri[3]={'f','r','w'};
char geri[3]={'b','c','k'};
char dur[3]={'d', 'u', 'r'};
char i=0,j=0;
void kurulum()
{
CMCON= 7;
TRISB= 2;
UART1_Init(2400);
delay_ms(100);
}
void main()
{
kurulum();
while(1)
{
for(i=0;i<15;i++)
{
UART1_Write(pre[i]);
}
for(j=0;j<10;j++)
{
for(i=0;i<3;i++)
{
while(!UART1_Tx_Idle());
UART1_Write(ileri[i]);
}
}
//*************************************************************
for(i=0;i<15;i++)
{
UART1_Write(pre[i]);
}
for(j=0;j<10;j++)
{
for(i=0;i<3;i++)
{
while(!UART1_Tx_Idle());
UART1_Write(geri[i]);
}
}
for(i=0;i<15;i++)
{
UART1_Write(pre[i]);
}
for(j=0;j<10;j++)
{
for(i=0;i<3;i++)
{
while(!UART1_Tx_Idle());
UART1_Write(dur[i]);
}
}
}
}
Receiver (Arduino):
// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem#airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $
#include <VirtualWire.h>
const int led_pin = 13;
const int receive_pin = 12;
void setup()
{
delay(1000);
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2400); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
pinMode(led_pin, OUTPUT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(led_pin, HIGH); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(' ');
}
Serial.println();
digitalWrite(led_pin, LOW);
}
}
I tried this code; but it didn't work...
There is another code;
void setup() {
Serial.begin(2400);
}
void loop() {
if (Serial.available() > 0){
Serial.println(Serial.read());
}
}
Before trying it; I connected data pin of receiver to RX pin of Arduino...
I usually got '0' byte.i
It didn't work as I desired...
SOLVED
The tests I have done so far were already taking the true datas but I was viewing them as numbers...
That's why I couldn't understand that It was working well.
Let's have a look at codes;
Transmitter:
The same as transmitter code at question message
Arduino (Receiver):
char x, msg[6];
int i= 0;
void setup() {
Serial.begin(2400);
}
void loop() {
if (Serial.available() > 0){
msg[i] = Serial.read();
if (msg[0]=='f' || msg[0] == 'b' || msg[0] == 'd'){
i++;
}
if (i==3){
Serial.println(msg);
i = 0;
msg[0]=0;
}
}
}
msg[0]=='f' || msg[0] == 'b' || msg[0] == 'd'
The purpose of comparison above is catching "frw", "bck" or "dur" messages which I sent transmitter...
The data pin of the receiver should be connected RX pin of the Arduino...

Out of time Timer5 interrupt Arduino

I have this code , I try to restart Timer5 when it out of time. But i can't, may you help me ?
#include <TimerFive.h>
#include <TimerOne.h>
#include <openGLCD.h>
#define OLDWAY
unsigned long timer5_started=0;
void setup() {
Serial.begin(9600);
Timer1.stop();
Timer1.detachInterrupt();
Timer1.initialize(500000);
Timer1.attachInterrupt(Timer1Handle);
Timer1.start();
Timer5.stop();
Timer5.detachInterrupt(); //detach interrupt
Timer5.initialize(100000);
Timer5.attachInterrupt(Timer5Handle);
Timer5.start();
///---
}
void loop() {
while(1){
Serial.println("loop...");
delay(500);
}
}
void Timer5Handle(){
timer5_started=millis();
Serial.println("timer 5...");
while(1){
int a=0;
}
}
void Timer1Handle(){
if(millis()-timer5_started>100000){
Serial.println("restart...");
Timer5.restart();
}
}
When Timer5 out of time, the Interupt is stop, how can i restart it ?
When Timer5 out of time, the Interupt is stop, how can i restart it ?
Well, I don't even understand what are you trying to do in that interrupt handler:
void Timer5Handle(){
timer5_started=millis();
Serial.println("timer 5...");
while(1){
int a=0;
}
}
However your interrupt handler is called in actual IRS, so everything else stops including all other Interrupts (including millis counting and delays). And because of infinite loop, it's forever. Interrupt handler should be as short as possible to ensure others are not missed or so.

arduino interrupts with servo motor

currently am working on project to open a door with access code using arduino UNO and a servo motor. Normal operation requires entering access code using keypad which is working fine. Another option requires pressing a button that causes an interrupt to rotate the servo motor. My problem is my interrupt only works once and never works again. Plus how do i put the for-loop to rotate the servo motor inside the interrupt function with a delay. I know that is not possible but am calling another function that has the delayMicroseconds but all this is not working. Below is my implementation please help
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo servo;
const int openButtonPin = 2;
void setup() {
// put your setup code here, to run once:
servo.attach(5);
pinMode(openButtonPin, INPUT); //Pin 2 is input
attachInterrupt(0, enforceOpenAccess, HIGH); // PIN 2
}
void(* resetFunc)(void) = 0;
void loop()
{
//My other keypad implementations go here
}
void myDelay(int x) // function to cause delay in the interrupt
{
for(int i = 0; i<x; i++)
{
delayMicroseconds(1000);
}
}
void enforceOpenAccess() // ISR
{
for(int k =0; k<=180; k+=2)
{
servo.write(k); //rotate the servo
myDelay(30); //delay the rotation of the servo
}
}
The code above is run on arduino UNO being simulated in proteus and the interrupt button is a push button. Please if there is other ways of implementing that but with the same behaviour as I have described above help out. Thanks a lot
There are a couple of problems in the slice of code you posted. Just for completeness, you should post the loop function, since we can't guess what you wrote inside.
Just one comment: did you put a pullup? Otherwise use INPUT_PULLUP instead of INPUT for the button pinmode.
The main one is that you attached the interrupt for the HIGH mode, which will trigger the interrupt any time the pin is up, not on the rising edge. And please use the macro digitalPinToInterrupt to map to the correct pin:
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
Then.. Let's improve the code. You really should use the interrupts only when strictly necessary when you have to respond IMMEDIATELY (= less than a couple of milliseconds) to an input. Here you don't have to, so it's MUCH better to check for the button in the loop (more on turning the motor following)
uint8_t lastState;
void setup()
{
...
lastState = LOW;
}
void loop()
{
uint8_t currentState = digitalRead(openButtonPin);
if ((currentState != lastState) && (currentState == HIGH))
{
// Start turning the motor
}
lastState = currentState;
...
}
This will enable you to properly debounce the button too:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT); //Pin 2 is input
debouncer.attach(openButtonPin);
debouncer.interval(5); // interval in ms
}
void loop()
{
debouncer.update();
if (debouncer.rose())
{
// Start turning the motor
}
...
}
If, on the other way, you REALLY want to use the interrupts (because waiting for a couple of milliseconds is too much for you), you should do something like this:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
}
void loop()
{
...
}
void enforceOpenAccess() // ISR
{
// Start turning the motor
}
It looks like your code? No, because now we'll speak about turning the motor
You should NOT use delays to make steps, because otherwise you will wait for 30ms * 180 steps = 5.4s before being able to do anything else.
You can, however, make a sort of reduced state machine. You want your servo to move from 0 to 180 in steps of 1. So let's code the "don't move" state with any value greater than 180, and consequently we can do something like this in the loop:
unsigned long lastServoTime;
uint8_t servoPosition = 255;
const int timeBetweenSteps_in_ms = 30;
void loop()
{
...
if (servoPosition <= 180)
{ // servo should move
if ((millis() - lastServoTime) >= timeBetweenSteps_in_ms)
{
lastServoTime += timeBetweenSteps_in_ms;
servoPosition++;
if (servoPosition <= 180)
servo.write(servoPosition);
}
}
}
Then, using any of the previous examples, instead of // Start turning the motor write
lastServoTime = millis();
servoPosition = 0;
servo.write(servoPosition);
This way you won't block the main loop even when the button is pressed
This is what is in my loop()
char key = keypad.getKey();
if(key)
{
if(j < 10)
{
studentNumber[j] = key;
//holdMaskedNumber[j] = '*';
lcd.setCursor(0,2);
lcd.print(String(studentNumber));
if(j == 9)
{
studentNumber[9] = '\0';
//holdMaskedNumber[9] = 0;
lcd.clear();
//String number = String(studentNumber);
//lcd.print(number);
//delay(1000);
//lcd.clear();
lcd.print("Access Code");
}
j++;
}
else
{
if(i < 5)
{
accessCode[i] = key;
holdMaskedCode[i] = '*';
lcd.setCursor(1,2);
lcd.print(String(holdMaskedCode));
if(i == 4)
{
holdMaskedCode[5] = '\0';
accessCode[5] = '\0';
//lcd.clear();
//lcd.setCursor(0,0);
//accessCodeString = String(accessCode);
//lcd.print(accessCodeString);
//delay(1000);
lcd.clear();
for(int i =0; i<6; i++)
{
lcd.print("Please wait.");
delay(500);
lcd.clear();
lcd.print("Please wait..");
delay(500);
lcd.clear();
lcd.print("Please wait...");
delay(500);
lcd.clear();
}
digitalWrite(4, HIGH);
lcd.print("Access Granted");
for(int k =0; k<=180; k+=2)
{
servo.write(k);
delay(30);
}
resetFunc();
}
i++;
}
}
}

Using SoftwareSerial as a private member variable

I'm trying to convert Sparkfun's example code for the SM130 RFID Reader into a class/library that's encapsulated nicely and can be used in multiple arduino sketches.
Sparkfun Example Code: https://github.com/sparkfun/RFID_Evaluation_Shield/blob/master/Firmware/RFID_Eval_13_56MHz.ino
Most of it went pretty smoothly, until I tried to make the Software Serial communications a member variable -- I keep getting errors like:
/Users/scottnla/Dropbox/arduino/libraries/SM130/SM130.cpp: In constructor 'SM130::SM130()':
/Users/scottnla/Dropbox/arduino/libraries/SM130/SM130.cpp:8: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
/Applications/Arduino.app/Contents/Resources/Java/libraries/SoftwareSerial/SoftwareSerial.h:83: note: candidates are: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
/Applications/Arduino.app/Contents/Resources/Java/libraries/SoftwareSerial/SoftwareSerial.h:48: note: SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
I found a stackoverflow post that seems to address this issue (Creating a Library for an Arduino), but their solution doesn't seem to work for me. The IDE's error messages seem to indicate that I can only use a 'base initializer' in a constructor; but this isn't what I want. What's the best way to deal with the problem that I'm having in my code?
Thanks!
Code below:
SM130.h
#ifndef SM130_h
#define SM130_h
#include<Arduino.h>
#include <WConstants.h>
class SM130 {
public:
SM130();
void connect(int RX, int TX);
void check_for_notag();
void halt_tag();
void parse_tag();
void print_serial();
void seek_tag();
void set_flag();
private:
int rfid_flag;
int data[11];
SoftwareSerial rfid;
};
#endif
SM130.cpp
#include <WProgram.h>
#include <SoftwareSerial.h>
#include "SM130.h"
SM130::SM130() {
rfid_flag = 0;
}
void SM130::connect(int RX, int TX) : rfid(RX, TX) {
if(!Serial.available()) {
Serial.begin(9600);
}
Serial.println("Connecting to SM130 RFID Reader...");
//rfid = SoftwareSerial(RX, TX);
rfid.begin(19200);
if(rfid.available()) {
rfid.println("Connected to SM130 RFID Reader!");
}
delay(10);
}
void SM130::check_for_notag() {
seek_tag();
delay(10);
parse_tag();
set_flag();
if(rfid_flag == 1) {
seek_tag();
delay(10);
parse_tag();
}
}
void SM130::halt_tag() {
rfid.write(0xFF);
rfid.write((byte)0x00); //manual typecasting needed for 0x00 to differentiate it from null pointer -- silly c compilers!
rfid.write(0x01);
rfid.write(0x93);
rfid.write(0x94);
}
void SM130::parse_tag() {
while(rfid.available()) {
if(rfid.read() == 0xFF) {
for(int i = 1; i < 11; i++) {
data[i] = rfid.read();
}
}
}
}
void SM130::print_serial() {
if(rfid_flag == 1) {
Serial.print(data[5],HEX);
Serial.print(data[6],HEX);
Serial.print(data[7],HEX);
Serial.print(data[8],HEX);
Serial.println();
}
}
void SM130::seek_tag() {
//insert hex tags here
rfid.write(0xFF);
rfid.write((byte)0x00); //manual typecasting needed for 0x00 to differentiate it from null pointer -- silly c compilers!
rfid.write(0x01);
rfid.write(0x82);
rfid.write(0x83);
}
void SM130::set_flag() {
if(data[2] == 6) {
rfid_flag++;
}
if(data[2] == 2) {
rfid_flag = 0;
}
}
My arduino sketch:
#include <SM130.h>
void read_serial();
SM130 rfidReader;
void setup() {
//Connect to computer
Serial.begin(9600);
//connect to SM130
rfidReader.connect(7,8);
}
void loop() {
read_serial();
}
void read_serial() {
rfidReader.seek_tag();
delay(10);
rfidReader.parse_tag();
rfidReader.set_flag();
rfidReader.print_serial();
delay(100);
}
/Users/scottnla/Dropbox/arduino/libraries/SM130/SM130.cpp: In
constructor 'SM130::SM130()':
/Users/scottnla/Dropbox/arduino/libraries/SM130/SM130.cpp:8: error: no
matching function for call to 'SoftwareSerial::SoftwareSerial()'
This part tells you everything. SoftwareSerial has no constructor without argument list, so you try to call a non-existing function.
If you don't specify a class member in constructor's initialization list, compiler by default calls () constructor for it. It is equivalent of:
SoftwareSerial ss;
if we weren't talking about classes.
http://www.cprogramming.com/tutorial/initialization-lists-c++.html
Unfortunately SoftwareSerial has no Set methods.
http://arduino.cc/de/Reference/SoftwareSerial
But worry not, dynamic memory allocation comes to the rescue! For example:
SM130.h
#ifndef SM130_h
#define SM130_h
#include<Arduino.h>
#include <WConstants.h>
class SM130 {
public:
SM130();
~SM130();
void connect(int RX, int TX);
void check_for_notag();
void halt_tag();
void parse_tag();
void print_serial();
void seek_tag();
void set_flag();
private:
int rfid_flag;
int data[11];
SoftwareSerial* rfid;
};
#endif
SM130.cpp (only some methods)
SM130::SM130() : rfid(NULL) {
rfid_flag = 0;
}
void SM130::connect(int RX, int TX) {
if(!Serial.available())
{
Serial.begin(9600);
}
Serial.println("Connecting to SM130 RFID Reader...");
if (NULL != rfid)
{
delete rfid;
}
rfid = new SoftwareSerial(RX, TX)
rfid->begin(19200);
if(rfid->available())
{
rfid->println("Connected to SM130 RFID Reader!");
}
delay(10);
}
~SM130::SM130()
{
if (NULL != rfid)
{
delete rfid;
}
}
I only posted my versions of constructor, connect and destructor (good to have it for avoiding memory leaks).
Don't forget about:
Checking if rfid is not NULL (better use NULL != rfid instead of rfid != NULL, because compiler will tell you about mistakes like if (rfid = NULL))
Changing rfid. to rfid->
You can even add disconnect method, which deletes old rfid and sets pointer to NULL:
void SM130::disconnect() {
delete rfid;
rfid = NULL;
}

Resources