how do I properly post a code for discussion?

Thread Starter

fredric58

Joined Nov 28, 2014
252
Hello, I have a code that Mr. Nick Gammon offered to us some years ago which I am studying. If I understand there is a right way and a wrong way to post them? I would like to post it because I am stuck on 1 particular part and would like some help with figuring it out, Thank you, Fred

Also, where would be the best place/forum to place it? Thanks again.
 

ericgibbs

Joined Jan 29, 2010
18,869
Hi,
If it is Program Code, use the Programming and Languages Forum.
If necessary, we can always relocate it.
Moderation.
 
Last edited:

Thread Starter

fredric58

Joined Nov 28, 2014
252
Thank you, I looked and it says to please use "coding tags". I did a search on how to use coding tags but did not see an explanation. could use please explain "coding tags"? Thank you, Fred
 

djsfantasi

Joined Apr 11, 2010
9,163
When you’re composing a post, there are several options along the top. One has a three dot icon. When you click it, more options appear. Click the “Code” option.
IMG_4712.jpeg

A new window will appear. Paste your code in the bottom field and click on “Continue”.
IMG_4713.png

This will insert your code with “coding tags”.

That’s it!
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
Well, I found it but......for some reason the line numbers do not show up when I paste it into my post and the lines I want highlighted do not highlight. This is a code for Arduino which is not listed in the "type" of code. Where am I failing? Thank you, Fred
 

WBahn

Joined Mar 31, 2012
30,077
Post a link to where you posted your code so that we can take a look.

I don't think there is a way to highlight or do other formatting within code tags. The main advantage that code tags provide is a monospaced "raw" format.
 

djsfantasi

Joined Apr 11, 2010
9,163
Well, I found it but......for some reason the line numbers do not show up when I paste it into my post and the lines I want highlighted do not highlight. This is a code for Arduino which is not listed in the "type" of code. Where am I failing? Thank you, Fred
The line numbers should only be used when highlighting. But to be honest, I don’t use that option

Also, I use one of the options for C for Arduino code, as the Arduino language is based on C++
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
I don't know how to do a link, however it is in programming and languages with the title "Question about timers ATtiny85 code and question"
 

ericgibbs

Joined Jan 29, 2010
18,869
hi @fredric58
Go to the thread, right Click on a post number [ top right}
Copy link, and Paste in this thread.
Mod.

Done:
https://forum.allaboutcircuits.com/threads/attiny85-timer-questions.164924/post-1451709

Added the Old Code, for reference,
C-like:
// ATtiny85 torch detector
// Author: Nick Gammon
// Date: 25 February 2015

// ATMEL ATTINY 25/45/85 / ARDUINO
// Pin 1 is /RESET
//
//                  +-\/-+
// Ain0 (D 5) PB5  1|    |8  Vcc
// Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1
// Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1
//            GND  4|    |5  PB0 (D 0) pwm0
//                  +----+

/*

  Pin 2 (PB3) <-- LDR (GL5539) --> Pin 7 (PB2) <----> 56 k <----> Gnd

  Pin 5 (PB0) <---- LED ---> 100 R <-----> Gnd

*/


#include <avr/sleep.h>    // Sleep Modes
#include <avr/power.h>    // Power management
#include <avr/wdt.h>      // Watchdog timer

const byte LED = 0;          // pin 5
const byte LDR_ENABLE = 3;   // pin 2
const byte LDR_READ = 1;     // Ain1 (PB2) pin 7
const int LIGHT_THRESHOLD = 200;  // Flash LED when darker than this

// when ADC completed, take an interrupt
EMPTY_INTERRUPT (ADC_vect);

// Take an ADC reading in sleep mode (ADC)
float getReading (byte port)
  {
  power_adc_enable() ;
  ADCSRA = bit (ADEN) | bit (ADIF);  // enable ADC, turn off any pending interrupt

  // set a2d prescale factor to 128
  // 8 MHz / 128 = 62.5 KHz, inside the desired 50-200 KHz range.

  ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2);

  if (port >= A0)
    port -= A0;
   
#if defined(__AVR_ATtiny85__)
  ADMUX = (port & 0x07);  // AVcc  
#else  
  ADMUX = bit (REFS0) | (port & 0x07);  // AVcc  
#endif

  noInterrupts ();
  set_sleep_mode (SLEEP_MODE_ADC);    // sleep during sample
  sleep_enable();

  // start the conversion
  ADCSRA |= bit (ADSC) | bit (ADIE);
  interrupts ();
  sleep_cpu ();    
  sleep_disable ();

  // reading should be done, but better make sure
  // maybe the timer interrupt fired

  // ADSC is cleared when the conversion finishes
  while (bit_is_set (ADCSRA, ADSC))
    { }

  byte low  = ADCL;
  byte high = ADCH;

  ADCSRA = 0;  // disable ADC
  power_adc_disable();

  return (high << 8) | low;

  }  // end of getReading

// watchdog interrupt
ISR (WDT_vect)
{
   wdt_disable();  // disable watchdog
}  // end of WDT_vect

#if defined(__AVR_ATtiny85__)
  #define watchdogRegister WDTCR
#else
  #define watchdogRegister WDTCSR
#endif

void setup ()
  {
  wdt_reset();
  pinMode (LED, OUTPUT);
  pinMode (LDR_ENABLE, OUTPUT);
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
  }  // end of setup

void loop ()
  {
  // power up the LDR, take a reading
  digitalWrite (LDR_ENABLE, HIGH);
  int value = getReading (LDR_READ);
  // power off the LDR
  digitalWrite (LDR_ENABLE, LOW);

  // if it's dark, flash the LED for 2 mS
  if (value < LIGHT_THRESHOLD)
    {
    power_timer0_enable ();
    delay (1);  // let timer reach a known point
    digitalWrite (LED, HIGH);
    delay (2);
    digitalWrite (LED, LOW);
    power_timer0_disable ();
    }

  goToSleep ();
  }  // end of loop

void goToSleep ()
  {
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  noInterrupts ();       // timed sequence coming up

  // pat the dog
  wdt_reset();

  // clear various "reset" flags
  MCUSR = 0;    
  // allow changes, disable reset, clear existing interrupt
  watchdogRegister = bit (WDCE) | bit (WDE) | bit (WDIF);
  // set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
  watchdogRegister = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0);    // set WDIE, and 2 seconds delay

  sleep_enable ();       // ready to sleep
  interrupts ();         // interrupts are required now
  sleep_cpu ();          // sleep              
  sleep_disable ();      // precaution
  }  // end of goToSleep
 
Last edited:
Top